Fibonacci Series in Python

Fibonacci Series in Python

3 mins read204 Views Comment
Vikram
Vikram Singh
Assistant Manager - Content
Updated on May 7, 2024 16:56 IST

Have you ever seen a cool spiral seashell? Believe it or not, there's a special math pattern behind it called the Fibonacci sequence! This sequence hides in nature and even helps computer programs work. This article will show you how to create the Fibonacci sequence using Python, a popular coding language. We'll start with the basic idea and then build a simple Python program to see this pattern come to life.

2023_07_Feature-Image-Templates-69.jpg

Fibonacci Series has fascinated mathematicians for centuries due to its unique properties and frequent appearance in nature and various mathematics and computer science fields. Fibonacci is named after the famous Italian mathematician Leonardo of Pisa, also known as Fibonacci. So, in this article, we will explore different methods to create the Fibonacci Series in Python.

Must Check: Python Online Course & Certification

Must Check: What is Python?

So, let’s start the article with the formal definition of the Fibonacci Series.

What is the Fibonacci Series?

Fibonacci numbers are the sequence of numbers defined by the linear equation:

Fn = Fn-1 + Fn-2, with

F0 =0,  F1 = F2 = 1, and n = 2, 3, ….

In simple terms, Fibonacci numbers are a sequence of numbers in which each number is a sum of the previous two.

Example

n 0 1 2 3 4 5 6 7 8 9
Fn 0 1 1 2 3 5 8 13 21 34
Recommended online courses

Best-suited Python courses for you

Learn Python with these high-rated online courses

– / –
40 hours
– / –
5 days
– / –
3 days
3 K
3 weeks
– / –
4 days
– / –
20 hours
– / –
2 months
Free
6 weeks

How To Implement Fibonacci Series in Python?

Fibonacci Series Using a Simple Loop in Python

Problem Statement: Find the first ten terms of the Fibonacci Series.


 
# Define the number of terms in the Fibonacci series you want to print
num_terms = 10
# The first two terms of the Fibonacci series
a, b = 0, 1
# This loop will run num_terms times
for _ in range(num_terms):
# Print the current term
print(a)
# Calculate the next term by adding the last two terms
temp = a + b
# Update the last two terms
a = b
b = temp
Copy code

Output

2023_07_fibonacci_series_using_loop.jpg

Fibonacci Series Using Recursion in Python

Problem Statement: Find the 10th number in the Fibonacci Series using Recursion.


 
def fibonacci(n):
# Base case: If n is 0 or 1, return n
if n <= 1:
return n
else:
# Recursive case: Return the sum of the previous two numbers in the series
return(fibonacci(n-1) + fibonacci(n-2))
# Test the function
print(fibonacci(10))
Copy code

Output

55

Fibonacci Program in C
Fibonacci Series Program in C++
Fibonacci Series in Java [Case-Study Based]

Fibonacci Series using Dynamic Programming in Python

Dynamic Programming, or DP, is a method to solve complex problems by breaking them down into simpler subproblems.

Problem Statement: Find the 50th number in the Fibonacci Series using Dynamic Programming in Python.


 
def fibonacci(n):
# Initialize a list to store the Fibonacci series
# The first two numbers in the Fibonacci series are 0 and 1
fib = [0, 1] + [0]*(n-1)
# Calculate each number in the series
for i in range(2, n+1):
# Each number is the sum of the previous two numbers
fib[i] = fib[i-1] + fib[i-2]
# Return the nth number in the series
return fib[n]
# Test the function
print(fibonacci(50))
Copy code

 

Output

12586269025

Case Study – Rabbit Population Growth

Problem Statement: Suppose you have a pair of rabbits, one male and one female. If every month, every pair of rabbits produces another pair, and rabbits start to breed when they are two months old, how many pairs of rabbits will you have after a year?

Let’s break down the problem and make it simpler to understand.
Let f(n) be the number of pairs during month n, and as given, we have a pair of rabbits, one male and one female. Every month, every pair of rabbits produces another pair, and rabbits start to breed when they are two months old. i.e.,
f(0) = 0,
f(1) = 1, for the first pair
f(2) = 1
The new pair is born at the end of month 2, so in the third month.
f(3) = 2
Since the new pair takes 2 months once they are of two months, therefore the initial pair will produce the baby in the third month only, i.e.,
f(4) = 3
In the next month, the initial pair as well as the month 2 pair breed will give birth, i.e., the total pairs will be:
f(5) = 5

Summarizing the above pattern will get:

n 0 1 2 3 4 5 6 7 8 9 10 11 12
Fn 0 1 1 2 3 5 8 13 21 34 55 89 144

So, there will be 144 pair of rabbits at the end of an year.

Now, let’s have a look how it can be done in Python.


 
def rabbit_pairs(n):
# The first two numbers in the Fibonacci series are 1 and 1
# This represents one pair of rabbits in the first and second month
a, b = 1, 1
# Calculate the number of rabbit pairs for each month
for _ in range(n - 1): # We subtract 1 because we have already defined the first two months
a, b = b, a + b
# Return the number of rabbit pairs in the nth month
return a
# Number of months in a year
months = 12
# Calculate and print the number of rabbit pairs after a year
print(rabbit_pairs(months))
Copy code

Output

144

Related Reads

Python NumPy Tutorial: For Beginners
Pandas vs Dask -Which One is Better?
Password Generator in Python
Exploring Python Pickle: The Ultimate Resource for Object Serialization and Storage
Learn the max() Function in Python with Examples
Types of Functions in Python
__init__ Method in Python
Understanding Self in Python
What is Capitalize in Python?

FAQs

What is Fibonacci Series?

Fibonacci numbers are the sequence of numbers defined by the linear equation, F(n) = F(n-1) + F(n-2), for n = 3, 4, ... and F(0) = 0, F(1) = F(2) = 1.

What are the first 10 Fibonacci Numbers in the Fibonacci Series?

The first 10 Fibonacci numbers are, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55.

What is the use of Fibonacci Series?

Fibonacci Series is used in Finance (Stock Market Analysis), Computer Science (Efficient Search), Music ( Tuning Musical Instruments), Art (Creating Pleasing Visual Design).

About the Author
author-image
Vikram Singh
Assistant Manager - Content

Vikram has a Postgraduate degree in Applied Mathematics, with a keen interest in Data Science and Machine Learning. He has experience of 2+ years in content creation in Mathematics, Statistics, Data Science, and Mac... Read Full Bio