Educatifu
Open menu

Nth Fibonacci Number

Easydynamic-programmingmath

Return the n-th Fibonacci number, where F(0) = 0, F(1) = 1 and F(k) = F(k-1) + F(k-2).

Input

A single integer n.

Output

F(n).

Examples

Example 1

Input
0
Output
0

F(0) = 0.

Example 2

Input
1
Output
1

F(1) = 1.

Example 3

Input
10
Output
55

F(10) = 55.

Constraints

  • 0 <= n <= 90 (F(90) still fits in 64-bit).

Hints

Reveal hint 1

Build up iteratively from F(0) and F(1) — no recursion needed.

Reveal hint 2

Keep only the last two values as you go.

Reference solution (spoiler — try it yourself first)

Approach

Iterate n times, keeping the previous two Fibonacci numbers (a, b) and stepping (a, b) → (b, a + b).

n = int(input())
a, b = 0, 1
for _ in range(n):
    a, b = b, a + b
print(a)

← All practice problems

Bring us the problem, not a perfect specification

Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.

  1. 01Share contextDescribe the workflow, constraint or risk.
  2. 02Clarify togetherWe identify missing facts and useful options.
  3. 03Choose a startAgree a focused assessment or delivery step.
Start a conversation