fibonacci series what Iam is missing ?
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Mon Mar 23 20:19:05 EDT 2015
On Tue, 24 Mar 2015 03:16 am, Chris Angelico wrote about the standard
recursive version of the Fibonacci series:
> On Tue, Mar 24, 2015 at 3:01 AM, Ganesh Pal <ganesh1pal at gmail.com> wrote:
>> def fib(n):
>> if n == 0:
>> return 0
>> elif n == 1:
>> return 1
>> else:
>> return fib(n-1) + fib(n-2)
> 2) Your algorithm is about as hopelessly inefficient as it could
> possibly be, barring deliberate intent.
It is pretty inefficient, but it is a good toy example of recursion. It's
also a good example of how *not* to write the Fibonacci series in practice,
what is mathematically straightforward is not always computationally
efficient.
The question is, just how inefficient is is? How many calls to `fib` are
made in calling fib(n)?
Answer to follow.
--
Steven
More information about the Python-list
mailing list