+ 1
How does it work?
def fib(x): if x==0 or x==1 return 1 else: return fib(x-1)+fib(x-2) print(fib(4)) print(fib(6)) print(fib(8)) >>> 5 13 34
3 Réponses
0
Thanks.
0
Erfan Bazri
fib(4)
= fib(4 - 1) + fib(4 - 2)
= fib(3) + fib(2)
= fib(3 - 1) + fib(3 - 2) + fib(2 - 1) + fib(2 - 2)
= fib(2) + fib(1) + fib(1) + fib(0)
= fib(2 - 1) + fib(2 - 2) + 1 + 1 + 1
= fib(1) + fib(0) + 1 + 1 + 1
= 1 + 1 + 1 + 1 + 1
= 5