Use tail recursion to calculate the Fibonacci series

Source: Internet
Author: User
In procedural programming, there are not many opportunities to solve problems using recursion. however, recursive method is a more intuitive and concise method to solve the problem, but the compiler has no special optimization on recursion. therefore, we can easily write less efficient recursive Programs. the so-called tail recursion is used for calculation during recursion. the following uses the Fibonacci series as an example to illustrate the differences between normal recursion and tail recursion.

Normal recursion:Public long maid (int n)
{
If (n = 1 | n = 2)
Return 1;
Else
Return maid (n-1) + maid (n-2 );
}

This implementation is simple and clear, that is, the execution speed is too slow, because the compiler performs computation as follows (for example, calculation of Fib (6): fib (6) = fib (5) + fib (4); = fib (4) + fib (3) + fib (3) + fib (2); = fib (3) + fib (2) + fib (2) + fib (1) + fib (2) + fib (1) + fib (2); = fib (2) + fib (1) + fib (2) + fib (2) + fib (1) + fib (2) + fib (1) + fib (2 ); = 8 from the preceding recursive expansion, we can see that fib (4) and FIB (3) have been calculated twice, and recursive functions have increased exponentially by 2. So it becomes very slow when the calculation reaches 30. Tail recursion:Public long maid (int n)
{
If (n = 1 | n = 2)
Return 1;
Else
Return tail (n, 1, 1, 3 );
} Private long tail (int n, long B1, long B2, int begin)
{
If (n = begin)
{
Return B1 + B2;
}
Else
Return tail (n, B2, B1 + B2, begin + 1 );
} Let's take a look at the computation process of fib_tail (3). fib_tail (6) = tail (6, 1, 2, 3) = tail (6, 2, 3, 4) = tail (6, 3, 5, 5) = 8 This calculation process is calculated (tail) every time the recursive function is entered, so it is a linear growth. As long as the compiler allows, we can calculate that both the fib_tail (100) are very fast. Normal cycle:Public long maid (int n)
{
Long b1 = 1, b2 = 1, temp, begin = 3;
For (; begin <= N; ++ begin)
{
Temp = b1;
B1 = B2;
B2 = temp + B2;
}
Return B2;
} Tail recursion usually occurs in functional programming, and the compiler of FP language is optimized. In fact, it is still possible to convert it into a loop form. However, recursive thinking is quite helpful.

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.