2017-12-26 19:11:23 +08:00
|
|
|
//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.
|
|
|
|
|
2017-09-24 04:15:29 +08:00
|
|
|
#include <iostream>
|
2017-12-26 19:11:23 +08:00
|
|
|
#include<cstdio>
|
2017-09-24 04:15:29 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2017-12-26 19:11:23 +08:00
|
|
|
const long long MAX = 93;
|
|
|
|
|
2017-09-24 04:15:29 +08:00
|
|
|
|
2017-12-26 19:11:23 +08:00
|
|
|
long long f[MAX] = {0};
|
|
|
|
|
2017-09-24 04:15:29 +08:00
|
|
|
|
2017-12-26 19:11:23 +08:00
|
|
|
long long fib(long long n)
|
2017-09-24 04:15:29 +08:00
|
|
|
{
|
|
|
|
|
2017-12-26 19:11:23 +08:00
|
|
|
if (n == 0)
|
|
|
|
return 0;
|
|
|
|
if (n == 1 || n == 2)
|
|
|
|
return (f[n] = 1);
|
|
|
|
|
|
|
|
|
|
|
|
if (f[n])
|
|
|
|
return f[n];
|
|
|
|
|
|
|
|
long long k = (n%2!=0)? (n+1)/2 : n/2;
|
|
|
|
|
|
|
|
f[n] = (n%2!=0)? (fib(k)*fib(k) + fib(k-1)*fib(k-1))
|
|
|
|
: (2*fib(k-1) + fib(k))*fib(k);
|
|
|
|
return f[n];
|
2017-09-24 04:15:29 +08:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:11:23 +08:00
|
|
|
|
2017-09-24 04:15:29 +08:00
|
|
|
int main()
|
|
|
|
{
|
|
|
|
//Main Function
|
2017-12-26 19:11:23 +08:00
|
|
|
for(long long i=1;i<93;i++)
|
2017-09-24 04:15:29 +08:00
|
|
|
{
|
2017-12-26 19:11:23 +08:00
|
|
|
cout << i << " th fibonacci number is " << fib(i) << "\n";
|
2017-09-24 04:15:29 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|