-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathNth-Fibonacci.cpp
63 lines (47 loc) · 1.24 KB
/
Nth-Fibonacci.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// This program calculates Nth- Fibonacci Number in Log(n) time
#include <iostream>
#include <vector>
using namespace std;
void multi(vector<vector<int64_t>> &v, vector<vector<int64_t>> m){
int64_t val_1 = (v[0][0] * m[0][0]) + (v[0][1] * m[1][0]);
int64_t val_2 = (v[0][0] * m[0][1]) + (v[0][1] * m[1][1]);
int64_t val_3 = (v[1][0] * m[0][0]) + (v[1][1] * m[1][0]);
int64_t val_4 = (v[1][0] * m[0][1]) + (v[1][1] * m[1][1]);
v[0][0] = val_1;
v[0][1] = val_2;
v[1][0] = val_3;
v[1][1] = val_4;
}
void powerCal(vector<vector<int64_t>> &v, int n){
if(n == 1 || n == 0){
return;
}
// Calculating V ^ (n/2)
powerCal(v, n/2);
// Multiplying matrices to get result
multi(v, v);
// In case n is odd
if(n & 1){
vector<vector<int64_t>> m{{1,1},{1,0}};
multi(v, m);
}
}
int64_t fibonacciNumber(int n){
vector<vector<int64_t>> v{{1,1}, {1,0}};
if(n == 0){
return 0;
}
else if(n == 1 || n == 2){
return 1;
}
powerCal(v, n-1);
return v[0][0];
}
// Driver Function
int main(){
int n;
cout << "Enter N : ";
cin >> n;
cout << "Nth Fibonacci Number : " << fibonacciNumber(n);
return 0;
}