Drill

ProblemsPython › warmup

Nth Fibonacci number

easywarmupPython

Return the nth number in the Fibonacci sequence, counting from zero.

fibonacci(n: int) → int

Solve it in the editor →

Where you start

def fibonacci(n: int) -> int:
    

Worked examples

CallResult
fibonacci(0)0
fibonacci(1)1
fibonacci(2)1
fibonacci(10)55

Hint

Keep two variables and slide them forward. No recursion needed.

Reference solution in Python
def fibonacci(n: int) -> int:
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a

The same problem in another language

More warmup problems in Python