Recursive Fibonacci sequence

First, what is the Fibonacci sequence?

Fibonacci number (Fibonacci sequence), also known as golden columns, because mathematician Leonardo Fibonacci (Leonardoda Fibonacci) as an example to the rabbit breeding and the introduction, it is also known as "Rabbit series," referring to such a series is: 1,1,2,3,5,8,13,21,34, ...... mathematically, Fibonacci numbers are listed in the following recursive method to define: F (1) = 1, F (2) = 1, F (n) = F (n-1) + F (n-2) (n> = 3, n∈N *)

By definition, this series from the beginning of paragraph 3, each of which is equal to the sum of the first two.
References Fibonacci column _ Baidu Encyclopedia

Second, given recursion Fibonacci sequence.

def fib(n):
return 1 and n <= 2 or fib(n - 1) +fib(n - 2)
print('\n the answer is %d'%(fib(10)))

Third, the scratch or python with recursive Fib (n), and tested, on your computer can calculate fib (10), fib (100) 1 minute, fib (1000), fib (10000) it?

1.fib(10)

When used 0.057 seconds

2.fib(100)

As shown, after waiting for more than three minutes still to no avail, I manually stopped its run (because of fan sound too great, not run ah!)

3.fib (1000) and fib (10000)

I did not try, but under the circumstances fib (100) point of view, should not run.

Fourth, the counterattack article!

After the reference to the website below:
Getting the python Fibonacci Recursive expression algorithms and non-recursive algorithm

At this point I found the site in non-recursive algorithm is given to solve the problem fib (n) soon

And understand the reason, the reason is:

  • 1. Recursive calculation complexity is: O (2 ^ N) ---- too time-consuming
  • 2. The non-recursive algorithm are time and space complexity is: O (N)

Run nonrecursive code calculates fib (100), fib (1000) and fib (10000)

fib(100)

fib(1000)

fib(10000)

Guess you like

Origin www.cnblogs.com/leo-skr/p/11842551.html