Problems

Fibonacci Number

easy
easy
math
dynamic-programming

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

Input / output

  • Input: n: integer
  • Output: integer F(n)

Examples

  1. n = 2 returns 1.
  2. n = 10 returns 55.

Constraints

  • 0 <= n <= 30

Follow-up Compare iteration, memoized recursion, fast doubling, and matrix exponentiation.

Examples

Example 1

Input: n = 2
Output: 1

Example 2

Input: n = 5
Output: 5

Example 3

Input: n = 10
Output: 55
🔒 5 hidden

Running will execute all 8 cases, including 5 hidden ones.