Home > Back-end >  The Fibonacci sequence output n number problem
The Fibonacci sequence output n number problem

Time:09-16

#include
using namespace std;
Int fib (int n) {
If (n==1) return 1;
If (n==2) return 1;
Return fib (n - 1) + fib (n - 2);
}
Int main ()
{
int n; Cin> n;
Coutreturn 0;
}
Code output time is too long, how should improve?

CodePudding user response:

 
Unsigned long long ffib (int n)
{
If (n & lt; 1) return 0;
If (n & lt; 3) return 1;
If (n & amp; 1) return - n/a=2, ffib ffib (n + 1) * (n + 1) + ffib * ffib (n) (n);
Return "n/a=2, ffib (n) * (ffib (n + 1) * 2 - ffib (n));
}


Can calculate to n=93 seconds, again big results will overflow the
  • Related