Menu
CoddyTech

Fibonacci Number

The Fibonacci numbers start with F(0) = 0 and F(1) = 1, and every later number is the sum of the two before it: F(n) = F(n-1) + F(n-2). The sequence begins 0, 1, 1, 2, 3, 5, 8, 13. Your function gets n and returns F(n).

Function

fib(n: integer) → integer
ninteger
the position in the Fibonacci sequence, counting from 0
Returnsinteger
the Fibonacci number F(n)

Constraints

  • 0 ≤ n ≤ 45
  • The answer fits in a signed 32-bit integer: F(45) = 1134903170.

Examples

Input
n = 4
Output
3
Explanation
Count up from the start: F(2) = 1 + 0 = 1, F(3) = 1 + 1 = 2, and F(4) = 2 + 1 = 3.

lock icon+13 hidden tests on Submit

challenge icon

Follow-up

Can you compute F(n) in O(log n) time?

Reset code
def fib(n):
    # Write code here
Test cases

Case 1

Case 2

Input

n = 4

Expected

3