AlgoFrames

Fibonacci, the naive way

Warm-up · Write code · about 6 min

Return the n-th Fibonacci number: fib(0) = 0, fib(1) = 1 and fib(n) = fib(n - 1) + fib(n - 2). Write it as a function that calls itself twice.

Example 1

Input:

[5]

Output:

5

Example 2

Input:

[10]

Output:

55

Constraints

  • 0 ≤ n ≤ 25

Learn the idea first: Recursion trees: Fibonacci and repeated work, a visual lesson.

More Recursion trees: Fibonacci and repeated work problems