0

Fibonacci - Recursion

I understand how the fibonacci sequence works, but when implementing a fibonacci function - I don't understand what's the given number to the function and what it returns. def fib(n): if n == 0: return 0 if n == 1: return 1 return fib(n - 1) + fib(n - 2)

30th Sep 2021, 12:51 PM
Yahel
Yahel - avatar
2 Réponses
0
Try working out this on pen and paper and visualuze it by tree diagram. ( try on few easy inputs like 1,2,3,4,5 ) You will soon start to see : f(n)=f(n-1)+f(n-2) for n>=2 and [ f(0)=0 , f(1)=1 ] That will be much benificial to you! feel free to ask if you aren't able to
30th Sep 2021, 12:54 PM
Prashanth Kumar
Prashanth Kumar - avatar
0
Recommend you to learn how recursion works.
30th Sep 2021, 1:47 PM
Mani_K_A