Last Updated : 22 Mar, 2025
To print the Fibonacci sequence in Python, we need to generate a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. The Fibonacci sequence follows a specific pattern that begins with 0 and 1, and every subsequent number is the sum of the two previous numbers.
Mathematically, the Fibonacci sequence can be represented as:
F(n) = F(n-1) + F(n-2)
Where:
F(0) = 0
F(1) = 1
F(n) for n > 1 is the sum of the two preceding numbers.
The sequence looks like this:
Using Naive Approach0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
This approach uses a while loop to print the first n Fibonacci numbers by repeatedly summing the previous two numbers. It starts with 0 and 1, and calculates the next number in the sequence until n terms are printed.
Python
n = 10
a = 0
b = 1
next = b
count = 1
while count <= n:
print(next, end=" ")
count += 1
a, b = b, next
next = a + b
print()
1 2 3 5 8 13 21 34 55 89
Explanation:
This approach iteratively calculates the nth Fibonacci number by updating two variables, a and b, without recursion, making it more efficient in terms of time and space.
Python
def fibonacci(n):
a = 0
b = 1
# Check if n is less than 0
if n < 0:
print("Incorrect input")
# Check if 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
print(fibonacci(9))
Explanation:
This approach uses dynamic programming by storing previously computed Fibonacci numbers in a list (FibArray). It avoids redundant calculations by checking if the result is already available, making it more efficient than the naive recursive approach.
Python
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]
print(fibonacci(9))
Explanation:
This approach uses Python’s lru_cache decorator from the functools module to cache the results of the Fibonacci function. It stores the results of expensive recursive calls, improving efficiency by avoiding redundant calculations, especially for larger values of n.
Python
from functools import lru_cache
@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)
print(fibonacci(9))
Explanation:
This approach uses backtracking with memoization to optimize the Fibonacci calculation. It stores previously computed values in a dictionary (memo), ensuring that each Fibonacci number is computed only once. This reduces the time complexity significantly compared to the naive recursive method.
Python
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]
print(fibonacci(9))
Explanation:
This approach uses recursion to calculate the Fibonacci sequence. The base cases handle inputs of 0 and 1 (returning 0 and 1 respectively). For values greater than 1, it recursively calls the function to sum the previous two Fibonacci numbers. While this method is straightforward, it is inefficient for larger inputs due to redundant calculations.
Python
def Fibonacci(n):
if n < 0:
print("Incorrect input")
elif n == 0:
return 0
elif n == 1 or n == 2:
return 1
else:
return Fibonacci(n-1) + Fibonacci(n-2)
print(Fibonacci(9))
Explanation:
Please refer complete article on the Program for Fibonacci numbers for more details!
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4