 GeeksforGeeks App
Open App Browser
Continue

# Python Program for Fibonacci numbers

The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation

`    Fn = Fn-1 + Fn-2`

with seed values

`   F0 = 0 and F1 = 1.`

Method 1 ( Use recursion ) :

## Python3

 `# Function for nth Fibonacci number``def` `Fibonacci(n):``  ` `    ``# Check if input is 0 then it will``    ``# print incorrect input``    ``if` `n < ``0``:``        ``print``(``"Incorrect input"``)` `    ``# Check if n is 0``    ``# then it will return 0``    ``elif` `n ``=``=` `0``:``        ``return` `0` `    ``# Check if n is 1,2``    ``# it will return 1``    ``elif` `n ``=``=` `1` `or` `n ``=``=` `2``:``        ``return` `1` `    ``else``:``        ``return` `Fibonacci(n``-``1``) ``+` `Fibonacci(n``-``2``)` `# Driver Program``print``(Fibonacci(``9``))` `# This code is contributed by Saket Modi``# then corrected and improved by Himanshu Kanojiya`

Output

`34`

Time complexity: O(2 ^ n)  Exponential
Auxiliary Space: O(n)

Method 2 ( Use Dynamic Programming ) :

## Python3

 `# Function for nth fibonacci``# number - Dynamic Programming``# Taking 1st two fibonacci numbers as 0 and 1``FibArray ``=` `[``0``, ``1``]` `def` `fibonacci(n):``  ` `    ``# Check is n is less``    ``# than 0``    ``if` `n < ``0``:``        ``print``(``"Incorrect input"``)``        ` `    ``# Check is n is less``    ``# than len(FibArray)``    ``elif` `n < ``len``(FibArray):``        ``return` `FibArray[n]``    ``else``:       ``        ``FibArray.append(fibonacci(n ``-` `1``) ``+` `fibonacci(n ``-` `2``))``        ``return` `FibArray[n]` `# Driver Program``print``(fibonacci(``9``))` `# This code is contributed by Saket Modi`

Output

`34`

Time complexity: O(n)
Auxiliary Space: O(n)

Method 3 ( Space Optimized):

## Python

 `# Function for nth fibonacci``# number - Space Optimisation``# Taking 1st two fibonacci numbers as 0 and 1` `def` `fibonacci(n):``    ``a ``=` `0``    ``b ``=` `1``    ` `    ``# Check is n is less``    ``# than 0``    ``if` `n < ``0``:``        ``print``(``"Incorrect input"``)``        ` `    ``# Check is n is equal``    ``# to 0``    ``elif` `n ``=``=` `0``:``        ``return` `0``      ` `    ``# Check if n is equal to 1``    ``elif` `n ``=``=` `1``:``        ``return` `b``    ``else``:``        ``for` `i ``in` `range``(``1``, n):``            ``c ``=` `a ``+` `b``            ``a ``=` `b``            ``b ``=` `c``        ``return` `b` `# Driver Program``print``(fibonacci(``9``))` `# This code is contributed by Saket Modi``# Then corrected and improved by Himanshu Kanojiya`

Output

`34`

Time complexity: O(n)
Auxiliary Space: O(1)

Method 4 (Cache):

## Python3

 `from` `functools ``import` `lru_cache` `# Function for nth Fibonacci number` `# lru_cache will store the result``# so we don't have to find``# fibonacci for same num again`  `@lru_cache``(``None``)``def` `fibonacci(num: ``int``) ``-``> ``int``:` `    ``# check if num is less than 0``    ``# it will return none``    ``if` `num < ``0``:``        ``print``(``"Incorrect input"``)``        ``return` `    ``# check if num between 1, 0``    ``# it will return num``    ``elif` `num < ``2``:``        ``return` `num` `    ``# return the fibonacci of num - 1 & num - 2``    ``return` `fibonacci(num ``-` `1``) ``+` `fibonacci(num ``-` `2``)`  `# Driver Program``print``(fibonacci(``9``))` `# This code is contributed by Sayedul Haque Sarker`

Output

`34`

Time complexity: O(n)
Auxiliary Space: O(n)

Method 5(Using Backtracking):

## Python3

 `def` `fibonacci(n, memo``=``{}):``    ``if` `n <``=` `0``:``        ``return` `0``    ``elif` `n ``=``=` `1``:``        ``return` `1``    ``elif` `n ``in` `memo:``        ``return` `memo[n]``    ``else``:``        ``memo[n] ``=` `fibonacci(n``-``1``) ``+` `fibonacci(n``-``2``)``        ``return` `memo[n]` `# Driver Program``print``(fibonacci(``9``))`

Output

`34`

Time complexity: O(n)
Auxiliary Space: O(n)

Please refer complete article on Program for Fibonacci numbers for more details!

My Personal Notes arrow_drop_up