Introduction to Fibonacci Series
The Fibonacci Series is a well-known mathematical sequence where each number is the sum of the two preceding ones. It is normally represented by the following sequence: 0, 1, 1, 2, 3, 5, 8, … To represent this sequence programmatically, Python is a favorite for teaching basic programming concepts. It has a beautiful logic that makes it a great tool for learning while creating efficient data structures. Ready to master Python logic? Take a look at our Python course syllabus today!
Fibonacci Series in Python
We’ll create a Python program to print the Fibonacci sequence in this article. You need to be knowledgeable with important python programming concepts like Python while Loop and Python if…else Statement to comprehend this example.
Fibonacci Series starts with the first two terms: 0 and 1. The two words before them are added to produce all other terms. This is to state the nth term is the sum of (n-1)th and (n-2)th term. Get the best Python Training in Chennai for a promising career in software development.
Method 1 – By utilizing a while loop
For printing the Fibonacci sequence, a while loop will be used.
Step 1: Enter the number of values from which the Fibonacci sequence should be generated.
Step 2: Set the count to 0, the n 1 to 0, and the n 2 to 1.
Step 3: If n terms is less than 0
Step 4: print “error” since the number is invalid for the series.
Step 5: The value of n 1 will be printed if n terms = 1.
Step 6: While count n terms
Step 7: Print (n 1
Step 8: nth = n 1 + n 2
Step 9: Up to the needed term, we shall update the variable by changing n 1 to n 2, n 2 to nth, and so on.
Example
n_terms = int(input (“How many terms is it to print? “))
# First two terms
n_1 = 0
n_2 = 1
count = 0
# Now, we will check if the number of terms is valid or not
if n_terms <= 0:
print (“Please enter a positive integer, the given number is not valid”)
# if there is only one term, it will return n_1
elif n_terms == 1:
print (“The Fibonacci sequence of the numbers up to”, n_terms, “: “)
print(n_1)
# Then we will generate Fibonacci sequence of number
else:
print (“The fibonacci sequence of the numbers is:”)
while count < n_terms:
print(n_1)
nth = n_1 + n_2
# At last, we will update values
n_1 = n_2
n_2 = nth
count += 1
Output
How many terms is it to print? 13
The Fibonacci sequence of the numbers is:
0
1
1
2
3
5
8
13
21
34
55
89
144
Method 2: Using Dynamic Programming
Example
# 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
Explore our Python tutorial for Beginners to learn further.
Method 3: Space Optimized
Example
# 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
Method 4: Cache
Example
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
Method 5: Using Backtracking
Example
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)
Conclusion
The Fibonacci Sequence is an odd set of numbers that originated in classical mathematics and has since been used in advanced mathematics, biology, statistics, computer science, and agile development. Join our software training institute in Chennai to earn your IBM Certification and Best Practices certification.
