← Back to challenges

Recursion: Fibonacci Numbers

PythonHardnumbersrecursion

Instructions

Fibonacci numbers are created in the following way:

F(0) = 0
F(1) = 1
...
F(n) = F(n-2) + F(n-1)

Write a function that calculates the nth Fibonacci number.

Examples

fib(0) ➞ 0

fib(1) ➞ 1

fib(2) ➞ 1

fib(8) ➞ 21

Notes

N/A

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Days in a Month