Fibonacci Series in Python
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.
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 |
Best-suited Python courses for you
Learn Python with these high-rated online courses
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 printnum_terms = 10
# The first two terms of the Fibonacci seriesa, b = 0, 1
# This loop will run num_terms timesfor _ 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
Output
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 functionprint(fibonacci(10))
Output
55
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 functionprint(fibonacci(50))
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 yearmonths = 12
# Calculate and print the number of rabbit pairs after a yearprint(rabbit_pairs(months))
Output
144
Related Reads
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).
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