Loading...
intermediate
Problem 06 • Step 06
fibonacci
The Fibonacci sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, …
fib(0)= 0fib(1)= 1fib(n)=fib(n - 1)+fib(n - 2)
This combines two things you’ve now done separately:
- Nested ifs from
sign—fibhas two base cases (n == 0andn == 1), so you need twoifexpressions, one inside the other’s else branch - Recursion from
sum-to— but wheresum-tohad one recursive call,fibhas two, added together
The new challenge is that second recursive call. In sum-to, the recursive case was n + sum-to(n - 1) — a value plus a recursive call. In fib, it’s fib(n - 1) + fib(n - 2) — two recursive calls combined with an operator. Both sides of the + are function calls.