The Fibonacci number refers to a series of values: 0, 1, 1, 2, 3, 5, 8, 13, 21 ,......
This series starts from the third number, and each subsequent number is obtained by the sum of the first two numbers.
We know that in programming we can use recursive and iterative methods to find the specified Fibonacci number, but these two methods have their own advantages and disadvantages.
Difference: the code written by recursive method (time complexity O (2 ^ n) is highly readable, which is equivalent to translating the mathematical formula in the book into code, but this method is too slow, when you calculate 50th Fibonacci numbers, your computer may have to compute for more than 10 minutes. In addition, recursion can easily cause stack overflow. Every time you call the evaluate function, you have to open up a space. The number of times that the 50th Fibonacci numbers call the function is terrible, similarly, the size of the space opened on the stack can be imagined, and stack overflow is normal.
Iteration Method: (time complexity O (N) is much more efficient than iteration method. iteration is obtained through loops. as long as three temporary variables are created, the Fibonacci number can be quickly obtained, and the speed is fast (for fear, the 100th Fibonacci number is just an instant), but the code of the iterative method is less readable, it is not easy for beginners to write such code.
---------------------
Tianzez
Source: csdn
Original: 78443321
Copyright Disclaimer: This article is an original article by the blogger. For more information, see the blog post link!
The following code is used:
int Fab(int n){ if(n == 0) return 0; if(n == 1) return 1; return Fab(n-1) + Fab(n-2);}
long long Fibnacci(unsigned n){ int result[2] = {0,1}; if(n < 2) return result[n]; long long fibNMinusOne = 0; long long fibNMinusTwo = 1; long long fibN = 0; for(int i = 2;i <= n;i++){ fibN = fibNMinusOne + fibNMinusTwo; fibNMinusOne = fibNMinusTwo; fibNMinusTwo = fibN; }}
Interview question 10: Fibonacci Series