On 03/23/2015 08:19 PM, Steven D'Aprano wrote:
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 <ganesh1...@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.
0 1
1 1
2 3
3 5
4 9
5 15
6 25
7 41
8 67
9 109
10 177
11 287
12 465
13 753
14 1219
--
DaveA
--
https://mail.python.org/mailman/listinfo/python-list