Fibonacci series recursion error

Jason Friedman jason at powerpull.net
Fri Apr 29 23:57:37 EDT 2011


> import os
> def fib(n):
>        if n == 1:
>          return(n)
>        else:
>          return (fib(n-1)+fib(n-2))
>
> list=fib(20)
> print(list)
>
> The above function return the
> return (fib(n-1)+fib(n-2))
>
>
> RuntimeError: maximum recursion depth exceeded in comparison
> [36355 refs]
>
> can any one help

The first call to fib() recursively calls fib() twice.  Each of those
will call fib() twice.  Each of those will call fib() twice.  Pretty
soon, you've got a lot of calls.

Have a look at:  http://en.literateprograms.org/Fibonacci_numbers_(Python).



More information about the Python-list mailing list