+ 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

21st Feb 2022, 2:34 PM
Erfan Bazri
Erfan Bazri - avatar
3 Answers
0
Thanks.
21st Feb 2022, 3:48 PM
Erfan Bazri
Erfan Bazri - avatar
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
21st Feb 2022, 4:43 PM
AÍąJ
AÍąJ - avatar