Iteration and recursive substitution Loop

Source: Internet
Author: User

In the book of computer programs and construction, we use recursion and iteration to replace loops. I have implemented it using C #. After verification, the results are correct. However, it is still a bit unclear about recursion and iteration, and we feel that recursion is also used in iteration. Please kindly advise.

This is the description of the recursive process in the book: the recursive process describes a fact in the syntax form, indicating that the process itself is referenced in the definition of this process (either directly or indirectly. However, when we say that a computing process has a certain pattern (such as linear recursion), we are talking about the progress of this computing process, rather than the syntax form written in the corresponding process. Therefore, we can say that a recursive process may not be easy to understand when an iterative computing process is generated. However, this computing process is indeed iterative, as shown in the following two examples, they are actually recursive processes, but their calculation process is indeed iterative.

1. Computing x + (x + 1) + (x + 2) + (x + 3) +... + y without loops

Recursion:

 

Code

 /// <Summary>
/// Use recursive summation
/// </Summary>
/// <Param name = "x"> start value </param>
/// <Param name = "y"> deadline </param>
/// <Returns> end value </returns>
Static int SumDigui (int x, int y)
{
If (x> y)
Return 0;
Else
{
Return y + SumDigui (x, y-1 );
}
}

 

 

 

Iteration Method (recursive process, but iterative calculation process ):

 

Code

 /// <Summary>
/// Iteration summation
/// </Summary>
/// <Param name = "x"> start value </param>
/// <Param name = "y"> deadline </param>
/// <Returns> and </returns>
Static int SumDiedai (int x, int y)
{
Return diedaiSum (x, x + 1, y );
}
/// <Summary>
/// Iteration summation
/// </Summary>
/// <Param name = "product"> result = product + next </param>
/// <Param name = "next"> </param>
/// <Param name = "maxcount"> </param>
/// <Returns> </returns>
Static int diedaiSum (int product, int next, int maxcount)
{
If (next> maxcount)
{
Return product;
}
Else
{
Return diedaiSum (product + next, next + 1, maxcount );

}
}

 

 

 

 

2. Calculate the factorial of n without loops

 

Recursion:

 

Code

 // Recursively calculate the factorial
Static int Jidigui (int n)
{
If (n = 1)
Return 1;
Else
{
Return n * Jidigui (n-1 );
}
}

 

 

Iteration Method (recursive process, but iterative calculation process ):

 

Code

 // Calculate the factorial by Iteration
Static int Jidiedai (int n)
{
Return factiter (1, 1, n );
}
/// <Summary>
/// Iterative Computing
/// </Summary>
/// <Param name = "product"> result = procduct * couter, initial value: 1 </param>
/// <Param name = "couter"> the counter initial value is 1 </param>
/// <Param name = "maxcount"> n </param>,
/// <Returns> </returns>
Static int factiter (int product, int couter, int maxcount)
{
If (couter> maxcount)
Return product;
Else
{
Return factiter (product * couter, couter + 1, maxcount );
}
}

 

 

3. Calculate the Fibonacci numbers 0, 1, 2, 3, 5, 8, and 13 by recursion and iteration respectively, 21 .........................

Recursive description:

If n = 0, Fib (n) = 0;

If n = 1, Fib (n) = 1;

Otherwise Fib (n) = Fib (n-1) + Fib (n-2 );

The procedure is as follows:

Code

  static int Fib(int n)
{
if (n == 0)
{ return 0; }
if (n == 1)
{ return 1; }
else
{
return Fib(n - 1) + Fib(n - 2);
}
}

Iteration description:

Set a basic integer a and a basic integer B. At initialization, a = Fib (1), B = Fib (0 );

Use the following conversion rules again:

A = a + B;

B =;

After applying the preceding transform Rule n again, a = Fib (n + 1) and B = Fib (n ).

In this case, B is the required value:

The Code is as follows:

Code

 // Calculate the Fibonacci number through iteration
Static int FibDiedai (int n)
{
Return FibIter (1, 0, n );
}
Static int FibIter (int a, int B, int count)
{
If (count = 0)
Return B;
Else
{
Return FibIter (a + B, a, count-1 );
}
}

 

From the above example, we can see that the status of the iterative computing process can be described by a fixed number of state variables, and there is a set of fixed rules to describe the transition of the computing process from a State to the next state, status variable update method, and there may be conditions to describe the termination of this calculation process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.