+ 6
Hi, Why the output is is 5 but not 7?
f(n-1) +f(n-2), f(5),f(5-1)+f(5-2)=7 Thank You in advance. https://code.sololearn.com/ce6vH6OUDxlq/?ref=app
2 Réponses
+ 6
Denise Roßberg ,Thank You very much for the competent answer.
+ 3
Hello Egor Tonchev(EGO)
Your code returns the fifth element of the fibonacci series.
f(1) = 1
f(2) = 1
f(3) = 2 (1+1)
f(4) = 3 (1+2)
f(5) = 5 (2+3)
f(6) = 8 (3+5)
f(7) = 13 (5+8)
As you can see each element is the sum of the two elements before.
n = 5
f(n-1) + f(n-2)
f(4) + f(3)
It does not mean 4+3 it means you call your function for n = 4 and n = 3
f(4) returns the value 3 and f(3) returns the value 2
-> f(4) + f(3) = 3 + 2 = 5
To understand it I suggest you to learn more about recursion (a function which calls itself).