The definition of the Fibonacci series:
Fibonacci Series(Italian: successione di Fibonacci), translatedThe number of feidanes,Fibonacci Series,Fibonacci Series,Golden split Series.
In mathematics,Fibonacci SeriesIt is defined by recursion:
In terms of words, it is the number of the Fibonacci series starting from 0 and 1, and the coefficients of the afterwards Fibonacci are summed up by the previous two numbers.
The first few Fibonacci coefficients are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,144,233 ......
Note:: 0 is not the first item, but the first item.
Below isSeveral common programming implementations of the Fibonacci series:
1. General Implementation:
int Fibonacci(int n){int a = 1, b = 1;if(n < 0){printf("The fibonacci number exists only with nonnegative index.\n");return -1;}else if (n == 0)return 0;else if(n==1 || n==2)return 1;else{for(int i=3; i<=n; i++){int tmp = b;b = a + b;a = tmp;}return b; }}
2. Recursive Implementation:
int Fibonacci(int n){if(n < 0)printf("The fibonacci number exists only with nonnegative index.\n");else{if(n == 0)return 0;else if(n == 1)return 1;elsereturn Fibonacci(n - 1) + Fibonacci(n - 2);}}
3. Iterative implementation:
int Fibonacci_iter(int a, int b, int count){if(count < 0)printf("The fibonacci number exists only with nonnegative index.\n");else if(count == 0)return b;elsereturn Fibonacci_iter(a + b, a, count - 1);}int Fibonacci(int n){return Fibonacci_iter(1, 0, n);}
4. Metadata programming implementation:
# Include <iostream> using namespace STD; int result; // master template <int n> // template class Fibonacci {public: Enum {result = Fibonacci <N-1> :: result + maid <N-2 >:: result}; // enumeration with implicit computation }; // fully-specific template <> class Fibonacci <1> // constructor with constant parameter 1 {public: Enum {result = 1 }; // assign the initial value 1} to the enumeration; // fully-specific template <> class Fibonacci <0> // template with parameter 0 {public: Enum {result = 0 }; // assign the initial value 0} to the enumeration; int main () {STD: cout <"the number of Fibonacci in the 20th item is:" <Fibonacci <20> :: result <STD: Endl; // implicit calculation system ("pause"); return 1 ;}
Summary:
Recursion (in mathematics and computer science) refers to the use of functions in the definition of functions.
Iteration: An Iteration in mathematics can be a process of Function Iteration, that is, the same function is used repeatedly for calculation. The result of the previous iteration is used as the input of the next iteration.
Note the following when using recursion:
1) recursion is to call itself in a process or function;
2) When recursion is used, there must be a clear recursion end condition called recursion exit.
Recursion is divided into two phases:
1) recurrence: solve complex problems to solve problems that are simpler than the original ones;
2) regression: When the simplest case is obtained, return gradually and obtain a complex solution in sequence.
Iteration: use the original value of the variable to calculate a new value of the variable. If recursion is self-called, iteration is a non-stop call B.
There must be iterations in recursion, but not necessarily in iteration. Most of them can be converted to each other. it can be used for iteration without recursion, recursively calling functions, wasting space, and recursion is too deep, which may easily cause stack overflow.
Metaprogramming refers to the compilation of a certain type of computer program. Such computer programs write or manipulate other programs (or themselves) as their data, or complete part of the work that should have been completed during compilation at runtime. In many cases, it is more efficient than writing all the code manually. The language used to write the metacharacter program is called the metacharacter language, and the language used to write the metacharacter program is called the target language. A language is also called reflection.
References:
The Fibonacci series: http://zh.wikipedia.org/wiki/%E6%96%90%E6%B3%A2%E9%82%A3%E5%A5%91%E6%95%B0%E5%88%97
Iteration: http://zh.wikipedia.org/wiki/%E8%BF%AD%E4%BB%A3
Recursion: http://zh.wikipedia.org/wiki/%E9%80%92%E5%BD%92
Difference between Recursion and iteration: http://blog.csdn.net/swliao/article/details/5337896
Recursion and iteration: http://blog.csdn.net/ljyf5593/article/details/5935795
There is a gap between the implementation of the recursive algorithm and iterative algorithm in C and Scheme: http://bbs.csdn.net/topics/190008464.
Several common implementations of the Fibonacci series