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.
Fibonacci Numbers using Native Approach
Fibonacci series using a Python while loop is implemented.
n = 10
num1 = 0
num2 = 1
next_number = num2
count = 1
while count < = n:
print (next_number, end = " " )
count + = 1
num1, num2 = num2, next_number
next_number = num1 + num2
print ()
|
Output
1 2 3 5 8 13 21 34 55 89
Python Program for Fibonacci numbers using Recursion
Python Function to find the nth Fibonacci number using Python Recursion.
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 ))
|
34
Time complexity: O(2 ^ n) Exponential
Auxiliary Space: O(n)
Fibonacci Sequence using DP (Dynamic Programming)
Python Dynamic Programming takes 1st two Fibonacci numbers as 0 and 1.
# Function for nth fibonacci # number 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 ))
|
34
Time complexity: O(n)
Auxiliary Space: O(n)
Optimization of Fibonacci sequence
Here, also Space Optimisation Taking 1st two Fibonacci numbers as 0 and 1.
# Function for nth fibonacci number 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 ))
|
34
Time complexity: O(n)
Auxiliary Space: O(1)
Fibonacci Sequence using Cache
lru_cache will store the result so we don’t have to find Fibonacci for the same num again.
from functools import lru_cache
# Function for nth Fibonacci number @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 ))
|
34
Time complexity: O(n)
Auxiliary Space: O(n)
Fibonacci Sequence using Backtracking
Function for nth Fibonacci number using Backtracking.
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 ))
|
34
Time complexity: O(n)
Auxiliary Space: O(n)
Please refer complete article on the Program for Fibonacci numbers for more details!