I remember that when we first learned the C language, every time we talked about recursion, whether in textbooks or teachers, we would give two typical examples of Recursive Implementation, one of which is factorial, the other is the Fibonacci series.
The code for calculating factorial using recursion is as follows:
// Recursive Calculation of factorial
Long factorial (int n)
{
If (n <= 0)
{
Return 1;
}
Return N * factorial (n-1 );
}
Such recursion is an obvious example of tail recursion. The so-called tail recursion is the last task of function execution, the function does not do anything after returning the result by recursive call. Tail recursion can be easily converted into a simple loop to complete the same task. In this way, a recursive implementation is converted into a non-Recursive Implementation.
The following is a non-Recursive Implementation of the factorial algorithm:
// Calculate the non-Recursive Implementation of factorial
Long factorial (int n)
{
Long result = 1;
While (n> 1)
{
Result * = N;
N --;
}
Return result;
}
From the perspective of this rewrite, we cannot see the significance of this rewrite. However, in some cases, recursion is not a good solution. The most common problem is that deep recursion may cause stack overflow errors! Sometimes recursion will also lead to a decrease in program efficiency. The following is a good inverse example of the series of Fibonacci.
The Fibonacci series are defined recursively (er... This definition is easy to induce us to use recursive forms for programming to solve this problem. The following is a Recursive Implementation of Fibonacci, which is easy to understand:
// Recursively calculate the value of the Fibonacci series
Typedef unsigned long ulong;
Ulong maid (int n)
{
If (n <= 2)
{
Return 1;
}
Return maid (n-1) + maid (n-2 );
}
Such a program is very simple. Sometimes, recursive program implementation looks much simpler than non-Recursive Implementation. At this time, you need to make some trade-off between efficiency and program readability and maintainability.
The Recursive Implementation of this Fibonacci triggers the other two recursion for each recursive call when calculating the value, and each of these two recursion triggers two recursive calls, the same is true for subsequent calls. Now, we know that this redundant recursive call increases exponentially. For example, during recursive Calculation of Fibonacci (10), the value of Fibonacci (3) is calculated 21 times. In recursive computation of Fibonacci (30), the number of calls is 317811 million! In fact, only one of these computations is necessary, and the rest is a waste!
The following program uses a simple loop to replace recursion. This non-recursive form is not as simple as the recursion given above (of course, this is also very simple) and does not conform to the recursive definition of Fibonacci. However, its efficiency has increased by several 100,000 times!
// Non-Recursive Implementation of the Fibonacci series
Typedef unsigned long ulong;
Ulong maid (int n)
{
Ulong result;
Ulong prev_result;
Ulong next_result;
Result = prev_result = 1;
While (n> 2)
{
N --;
Next_result = prev_result;
Prev_result = result;
Result = prev_result + next_result;
}
Return result;
}
Therefore, the conclusion in this article is that before you implement a function using recursion, you should first ask yourself whether the benefit of recursion is worth the cost. Of course, when programming, whatever implementation method, we should ask ourselves this question.
Note: For details about this article, refer to the typical book "C and pointer" (pointers on C ).
Fibonacii non-recursion