← Recursion

Fibonacci

medium

Compute the nth Fibonacci number recursively, where fib(0) = 0, fib(1) = 1, and every later term is the sum of the two before it.

This needs two base cases instead of one, and – new in this exercise – a recursive case that calls itself twice, not once, each on a smaller input. Try tracing fibonacci(4) by hand, the way the lesson traced factorial(5): you’ll notice the call tree branches into two calls at every level, and that fibonacci(2) in particular ends up computed more than once along the way – worth noticing now, even though fixing that inefficiency is out of scope for this exercise.

Your solution

"Run" uses the sample stdin ("10"). "Submit" checks your code against all 4 test cases.

Test results

Submit your solution to run it against all test cases.

Hints

    Reference solution