The Fibonacci numbers are recursively defined: For notation, let F(n) stand for the nth Fibonacci number. Then the recursive definition is: F(n+2) = F(n+1) + F(n) where F(1) = 1 and F(2) = 1.
So for example, in order to find the 4th Fibonacci number, we have that F(4) = F(3) + F(2).
But F(3) = F(2) + F(1) where F(2) = 1 and F(1) = 1.
So after substitution, we have that F(4) = F(2) + F(1) + F(2) = 1 + 1 + 1 = 3.
Think of it that way and it might help you understand the Fibonacci sequence and then the code.
-Tom
Edited for clarity
So for example, in order to find the 4th Fibonacci number, we have that F(4) = F(3) + F(2).
But F(3) = F(2) + F(1) where F(2) = 1 and F(1) = 1.
So after substitution, we have that F(4) = F(2) + F(1) + F(2) = 1 + 1 + 1 = 3.
Think of it that way and it might help you understand the Fibonacci sequence and then the code.
-Tom
Edited for clarity
