Fibonacci Series in Python

Fibonacci Series in Python

3 mins read204 Views Comment
clickHere
Vikram Singh
Assistant Manager - Content
Updated on Mar 27, 2024 07:50 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.

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: 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

How To Implement Fibonacci Series in Python?

Fibonacci SeriesUsing a Simple Loopin 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 = tempCopy code```

Output

Fibonacci SeriesUsing Recursionin 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))Copy code```

Output

55

Fibonacci Program in C
The Fibonacci Series is a sequence of numbers where each number is a sum of the previous two numbers. This article will explore how to implement the Fibonacci Series in...read more
Fibonacci Series Program in C++
The Fibonacci series is an intriguing mathematical idea that is popular for practicing in Python, especially for those just starting out.
Fibonacci Series in Java [Case-Study Based]
The Fibonacci series is a sequence of numbers in which each number is the sum of the two preceding ones, usually starting with 0 and 1. In Java, the Fibonacci...read more

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))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:

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))Copy code```

Output

144

Python NumPy Tutorial: For Beginners
This article talks about NumPy. How to install it? It also covers Input/Output, creating and Manipulating DataArray Operations, mathematical and statistical Functions with NumPy, You will learn reshaping, Joining, and...read more
Pandas vs Dask -Which One is Better?
Find the main differences between Pandas and Dask.
Password Generator in Python
This is a quick guide for those who want to generate strong password using python. This article includes step by step explanation of programming and included all steps needed to...read more
Exploring Python Pickle: The Ultimate Resource for Object Serialization and Storage
In this article, we will briefly discuss how to use the Python pickle module with the help of examples. We will also discuss some of the best practices for using...read more
max() Function in Python
This article includes introduction to max() Function in Python and Examples of Using the max() Function in Python.
Types of Functions in Python
This article explains different types of Functions in Python.The functions explained are Built-in Functions, User-defined Functions, Recursive Functions, Lambda Function.
__init__ Method in Python
In this article we will learn about __init__ Method,Inheritance of __init__ Method,calling __init__ Method.This topic is covered with examples in python.
Understanding Self in Python
In Python, the term “self” refers to the custom class used to access the class’s members and methods, as well as to create new members. In the class constructor and...read more
What is Capitalize in Python?
The purpose of this blog is to explain you the concept of Capitalize in Python. We have explained this concept with the help of relevant examples. Let’s explore.

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).