← Back to challenges

Fast Fibonacci

PythonHardalgorithmsloopsmath

Instructions

In mathematics, the Fibonacci numbers, commonly denoted Fn, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1:

Fibonacci Sequence

and

Fibonacci Sequence 2

for n > 1

The beginning of the sequence is thus:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

The function fib_fast(num) returns the fibonacci number Fn, of the given num as an argument.

Examples

fib_fast(5) ➞ 5

fib_fast(10) ➞ 55

fib_fast(20) ➞ 6765

fib_fast(50) ➞ 12586269025

Notes

  • The input number is always positive.
  • Innokodakademija would fail your code if it's not Fast enough.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.