Careful use of recursive Fibonacci Recursive Implementation Analysis

Source: Internet
Author: User
Tags benchmark


[Fibonacci Function Definition]

The Fibonacci series, also known as the Golden split series, refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21 ,...... In mathematics, the Fibonacci series are defined in a recursive method as follows: f0 = 1, F1 = 1, FN = f (n-1) + f (n-2) (N> = 2, n, N *).

[Disadvantages of using recursive solutions to the Fibonacci function]

The Fibonacci function uses recursion to implement the following code:


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/47/95/wKioL1P8jbyiUwf8AABfC3Xjldc436.jpg "Title =" 21:03:38 screen .png "alt =" wkiol1p8jbyiuwf8aabfc3xjldc436.jpg "/>



It is very inefficient to calculate the natural recursion program of Fibonacci. Although the above program is simple in writing, it seems that recursion is wise to use, but it is actually inefficient. Especially when n = 40, the efficiency attenuation is particularly obvious. To calculate fib (N), a call to fib (n-1) and FIB (n-2) is required. However, because f fib (n-1) is calculated) it also needs to call fib (n-2) and FIB (n-3) recursively. Therefore, FIB (n-2) is computed twice to track the entire algorithm, it can be found that fib (n-3) is calculated 33 times, FIB (n-4) is calculated 55 times, and FIB (n-5) is calculated 88 times.

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/47/95/wKioL1P8ki_CuEfFAADpqGfRtwg006.jpg "Title =" 21:50:45 screen .png "alt =" wkiol1p8ki_cueffaadpqgfrtwg006.jpg "/>


Legend-recursive calculation of the number of tracking Fibonacci

Low program efficiency is caused by a large amount of redundant work, which violates the synthetic benefit law (the same example for solving a problem is, do not perform repetitive work in different recursive calls). When calling fib (n-1) for the first time, FIB (n-2) is actually calculated somewhere ), FIB (n-2) information is not stored, resulting in Repeated Computation when fcall uses fib (n-2, in this way, recursive redundant computing leads to a huge running time.


[Replace recursion with For Loop Calculation]

The Calculation of Fib (n) requires fib (n-1) and FIB (n-2). Therefore, the recent two Fibonacci numbers are recorded, to avoid repeated computation. The code for calculating the Fibonacci number in a for loop is as follows:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/47/93/wKiom1P8kiqAWIQIAADDRKL9IcQ935.jpg "Title =" 21:55:33 screen .png "alt =" wkiom1p8kiqawiqiaaddrkl9icq935.jpg "/>

[Use four basic principles of recursion]

When writing a program using recursion, follow the following four rules, otherwise it is easy to write a program with low efficiency.

11. baseline. There must always be some benchmark situations that can be solved without recursion.

2. Continuous promotion. for situations where recursive solutions are required, each recursive call must push the situation to a benchmark situation.

3. Design Rules. Assume that all recursion operations can be performed.

4. synthetic benefit rules. When solving the same strength of a problem, do not perform repetitive work in different recursive calls.



Careful use of recursive Fibonacci Recursive Implementation Analysis

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.