mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #75 from hegdenaveen1/patch-7
Updated logic of fibonacci.cpp
This commit is contained in:
commit
489de17556
@ -1,99 +1,46 @@
|
|||||||
|
//An efficient way to calculate nth fibonacci number faster and simpler than O(nlogn) method of matrix exponentiation
|
||||||
|
//This works by using both recursion and dynamic programming.
|
||||||
|
//as 93rd fibonacci exceeds 19 digits, which cannot be stored in a single long long variable, we can only use it till 92nd fibonacci
|
||||||
|
//we can use it for 10000th fibonacci etc, if we implement bigintegers.
|
||||||
|
//This algorithm works with the fact that nth fibonacci can easily found if we have already found n/2th or (n+1)/2th fibonacci
|
||||||
|
//It is a property of fibonacci similar to matrix exponentiation.
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include<cstdio>
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
const long long MAX = 93;
|
||||||
|
|
||||||
|
|
||||||
/*
|
long long f[MAX] = {0};
|
||||||
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;
|
|
||||||
.
|
|
||||||
.
|
|
||||||
.
|
|
||||||
.
|
|
||||||
*/
|
|
||||||
long long a[2][2] = {{1,1},{1,0}};//fibonacci matrix
|
|
||||||
long long ans[2][2] = {{1,1},{1,0}};//final ans matrix
|
|
||||||
/*
|
|
||||||
Working Principal:
|
|
||||||
|
|
||||||
[F(k+1) F(k)] [1 1]^k
|
long long fib(long long n)
|
||||||
| | = | |
|
|
||||||
[F(k) F(k-1)] [1 0]
|
|
||||||
|
|
||||||
where F(k) is the kth term of the Fibonacci Sequence.
|
|
||||||
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
void product(long long b[][2],long long k[][2])
|
|
||||||
{
|
{
|
||||||
/*
|
|
||||||
Function for computing product of the two matrices b and k
|
|
||||||
and storing them into the variable ans.
|
|
||||||
|
|
||||||
Implementation :
|
if (n == 0)
|
||||||
Simple matrix multiplication of two (2X2) matrices.
|
return 0;
|
||||||
*/
|
if (n == 1 || n == 2)
|
||||||
long long c[2][2];//temporary stores the answer
|
return (f[n] = 1);
|
||||||
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(long long n)
|
if (f[n])
|
||||||
{
|
return f[n];
|
||||||
|
|
||||||
/*
|
long long k = (n%2!=0)? (n+1)/2 : n/2;
|
||||||
Function for calculating A^n(exponent) in a recursive fashion
|
|
||||||
|
f[n] = (n%2!=0)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
|
||||||
Implementation:
|
: (2*fib(k-1) + fib(k))*fib(k);
|
||||||
A^n = { A^(n/2)*A^(n/2) if n is even
|
return f[n];
|
||||||
{ 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()
|
int main()
|
||||||
{
|
{
|
||||||
//Main Function
|
//Main Function
|
||||||
cout <<"Enter the value of n\n";
|
for(long long i=1;i<93;i++)
|
||||||
long long n;
|
|
||||||
cin >>n;
|
|
||||||
if(n == 1)
|
|
||||||
{
|
{
|
||||||
cout<<"Ans: 0"<<endl;
|
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
power_rec(n-1);
|
|
||||||
cout <<"Ans :"<<ans[0][1]<<endl;
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user