forked from TheAlgorithms/C-Plus-Plus
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fibonacci.cpp
100 lines (84 loc) · 1.61 KB
/
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
using namespace std;
/*
Efficient method for finding nth term in a fibonacci number sequence.
Uses Dvide and conquer approach
Enter Values from 1
Eg-
1st term = 0;
2nd term = 1;
3rd term = 1;
.
.
.
.
*/
int a[2][2] = {{1,1},{1,0}};//fibonacci matrix
int ans[2][2] = {{1,1},{1,0}};//final ans matrix
/*
Working Principal:
[F(k+1) F(k)] [1 1]^k
| | = | |
[F(k) F(k-1)] [1 0]
where F(k) is the kth term of the Fibonacci Sequence.
*/
void product(int b[][2],int k[][2])
{
/*
Function for computing product of the two matrices b and k
and storing them into the variable ans.
Implementation :
Simple matrix multiplication of two (2X2) matrices.
*/
int c[2][2];//temporary stores the answer
c[0][0] = b[0][0]*k[0][0]+b[0][1]*k[1][0];
c[0][1] = b[0][0]*k[0][1]+b[0][1]*k[1][1];
c[1][0] = b[1][0]*k[0][0]+b[1][1]*k[1][0];
c[1][1] = b[1][0]*k[0][1]+b[1][1]*k[1][1];
ans[0][0] = c[0][0];
ans[0][1] = c[0][1];
ans[1][0] = c[1][0];
ans[1][1] = c[1][1];
}
void power_rec(int n)
{
/*
Function for calculating A^n(exponent) in a recursive fashion
Implementation:
A^n = { A^(n/2)*A^(n/2) if n is even
{ A^((n-1)/2)*A^((n-1)/2)*A if n is odd
*/
if((n == 1)||(n==0))
return;
else
{
if((n%2) == 0)
{
power_rec(n/2);
product(ans,ans);
}
else
{
power_rec((n-1)/2);
product(ans,ans);
product(ans,a);
}
}
}
int main()
{
//Main Function
cout <<"Enter the value of n\n";
int n;
cin >>n;
if(n == 1)
{
cout<<"Ans: 0"<<endl;
}
else
{
power_rec(n-1);
cout <<"Ans :"<<ans[0][1]<<endl;
}
return 0;
}