Loading practice problems…
Loading your practice workspace…
Loading code editor…
10 / 29
Sign in
Algorithms/Recursion

Fibonacci Number

easy20 min

The Fibonacci sequence starts with F(0) = 0 and F(1) = 1. Every later value is the sum of the previous two values.

Given n, return F(n) using a recursive solution.

Example 1

Inputn = 2

Output1

The first two values are F(0) = 0 and F(1) = 1, so F(2) = 0 + 1 = 1.

Example 2

Inputn = 6

Output8

Each value is the sum of the previous two. F(4) = 3 and F(5) = 5, so F(6) = 3 + 5 = 8.

Constraints

  • 0 ≤ n ≤ 30
  • Use recursion; memoization is allowed.
RecursionMemoization