+ 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))

14th Jan 2019, 5:51 PM
Robert Williams
Robert Williams - avatar
2 Respuestas
+ 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
14th Jan 2019, 6:00 PM
Diego
Diego - avatar
+ 2
Thank you Diego Acero!
14th Jan 2019, 6:02 PM
just trying to think
just trying to think - avatar