+ 2
Python.. output is 5. I cannot figure out how to step through this recursive function in my head and get it. Help?
def fb(n): if n==0 return 0 if n==1: return 1 else: return(fb(n-1)+fb(n-2)) print(fb(5))
2 ответов
+ 2
fb(0) == 0
fb(1) == 1
fb(n) == fb(n-1) + fb(n-2) for n > 1
fb(5)
== fb(4) + fb(3)
== fb(3) + fb(2) + fb(2) + fb(1)
==fb(2) + fb(1) + fb(1) + fb(0) + fb(1) + fb(0) + fb(1)
== fb(1) + fb(0) + fb(1) + fb(1) + fb(0) + fb(1) + fb(0) + fb(1)
== 1 + 0 + 1 + 1 + 0 + 1 + 0 + 1
== 5
+ 2
Thank you Diego Acero!