+ 1
Answer 13, but I don't understand how it calculated, what order of action? "fibo(x-1) + fibo(x-2)"
#recursion, fibonacci def fibo(x): if x==0: return 0 elif x==1: return 1 else: return fibo(x-1) + fibo(x-2) print(fibo(7))
3 Réponses
+ 13
from left to right you have to move, here is example for f(3) :
f(3)
/ \
f(2) f(1)
/ \ \
f(1) f(0) 1
/ \
1 0
from above diagram, f(0)=0, f(1)=1, f(2)=1+0=1, f(3)=f(2)+f(1)=1+1=2
similarily you can make for f(7)
in simple words each term is sum of previous 2 terms, so you can easily make series as you already know 1st two terms:
0, 1, 1, 2, 3, 5, 8, 13
|
f(7)
+ 5
if x==3 you have
fibo(3)==fibo(2)+fibo(1)==
==fibo(0)+fibo(1)+1==
==0+1+1
For 7 the same
0
Thanks!