5 Methods To Write Fibonacci Series In Python

5 Methods To Write Fibonacci Series In Python

fibonacci series in python

5 Methods To Write Fibonacci Series In Python

5 Methods To Write Fibonacci Series In Python

5 Methods To Write Fibonacci Series In Python

Telegram Group Join Now
WhatsApp Channel Join Now
YouTube Channel Subscribe

5 Methods To Write Fibonacci Series In Python

5 Methods To Write Fibonacci Series In Python

If you are interested in mathematics, you already know about the Fibonacci series. The Fibonacci series is, in basic terms, a set of numbers. The series is named after the Italian scientist Leonardo Fibonacci. So basically, mathematicians and scientists have been interested in this series for hundreds of years since Leonardo Fibonacci wrote about it in his book “Liber Abaci” in 1202.

In this blog post, we’ll explore the logic behind the Fibonacci series and 5 methods to write fibonacci series in python.

So let’s first understand what exactly is the Fibonacci series?


Enroll now and take the first step towards a successful career. Click here to join our courses today!


What is the Fibonacci Series?

The Fibonacci numbers are the numbers in a series that starts with 0 and 1. This series is called the Fibonacci sequence, and each number after the first two is the sum of the two numbers before it. 

Hence the series is looks like this:-

1123581321345589…….goes on like that.

The basic formula of Fibonacci series is – 

F(n)=F(n−1)+F(n−2)

So this formula is just a way of expressing the idea about fibonacci series.

Let’s take an simple example so you get better understanding of this formula – 

Imagine you have a series of numbers:

0,1,1,2,3,5,8,13,…

As we discussed earlier, the fibonacci series starts with 0 & 1.

  • The third number (1) is the sum of the first two numbers (0 + 1).
  • The fourth number (2) is the sum of the second and third numbers (1 + 1).
  • The fifth number (3) is the sum of the third and fourth numbers (1 + 2).
  • The Sixth number (5) is the sum of the fourth and fifth numbers (2 + 3).
  • The Seventh number (8) is the sum of the fifth and sixth numbers (3 + 5).
  • The eighth number (13) is the sum of the seventh and eighth numbers (8 + 13).
  • And so on …..

Also Read: How Long Does It Take To learn Python


Fibonacci Series In Python

There are various methods to write Fibonacci series programs. In this blog I will explain 5 methods to write fibonacci series, which are the most easy methods.

Method 1: for Loop

Method 2: Recursion

Method 3: if else statement

Method 4: Matrix Exponentiation

Method 5: Memorization

 

Let’s explore one by one method with coding examples.

Method 1: Fibonacci Series Using for Loop

To generate the Fibonacci series using a for loop in Python, first we have to initialize ‘x’ and ‘y’ to 0 and 1. 

Such as – 

x=0

y=1

After a defined number of iterations, the loop sums ‘x’ and ‘y’ to create the next term. 

With each iteration, the result is printed. Repeatedly adding the two numbers before it in this simple loop makes the Fibonacci series very quickly.

Input – 

O/p:

0

1

1

2

3

5

8

13

21

34


Also Read: What is Python Programming Language


Method 2: Fibonacci Series Using recursion

A function recursively defines itself in programming. It solves problems by first solving smaller versions of the same problem. Recursion is when a Python function calls itself.

Input: 

  • Defines a function “fibonacci_recursive” that calculates Fibonacci numbers.
  • If “n” is 0 or 1, immediately returns n
  • Otherwise, add “n-1” and “n-2” recursive calls to get n’s Fibonacci number.
  • Uses a loop to print the first 10 terms of the Fibonacci series

Output :

0

1

1

2

3

5

8

13

21

34


Also Read: API Integration In Python


Method 3: Fibonacci Series Using If else Statement

Input: 

  • For each iteration of the loop, the current term (x) is printed.
  • If there are more terms to generate (i < n – 1 is true), the values of x and y are updated to generate the next term.
  • The loop continues until the specified number of terms (n) is reached.

O/p: 

0

1

1

2

3

5

8

13

21

34


Also Read: Python Identity Operator


Method 4: Fibonacci Series Using Matrix Exponentiation

Input : 

  • The function matrix_exponentiation takes an integer n as input.
  • It checks for base cases: if n is 0, the function returns 0, and if n is 1, it returns 1.
  • The base matrix [1110] is used. The Fibonacci sequence is related to the powers of this matrix.
  • np.linalg.matrix_power is used to calculate the matrix raised to the power of n – 1.
  • The Fibonacci number is then obtained as the element at position (0, 0) in the result matrix.
  • The example uses a loop to print the first 10 Fibonacci numbers.

Output: 

0

1

1

2

3

5

8

13

21

34


Method 5: Fibonacci Series Using Memorization

Input : 

  • The function matrix_exponentiation takes an integer n as input.
  • When n is 0, the function returns 0, and when n is 1, it returns 1.
  • The Fibonacci transformation base matrix is [1110].
  • A matrix raised to the power of n-1 is calculated using np.linalg.matrix_power. 
  • From the result matrix, the nth Fibonacci number is retrieved at position (0, 0).
  • The loop prints the first 10 Fibonacci numbers.

Output : 

0

1

1

2

3

5

8

13

21

34


Conclusion

  • I hope you get a better understanding of “Fibonacci Series in Python” .
  • We used 5 simple methods for Loop, Recursion, if else statement, Matrix Exponentiation
  • and Memorization
  • Python offers many ways to create this sequence, including using iterative loops, recursive functions, and advanced matrix exponentiation. These examples show how beautiful mathematical ideas can be in programming.

Want to start a career in Python Programming call us +91 8600998107 / +91 7028710777 For more details.


FAQ’s

How do you add a Fibonacci sequence in Python?

To add a Fibonacci sequence in Python, use two methods – 

  1. Fibonacci Series using loop 
  2. Fibonacci series using recursion

What is the equation for the Fibonacci sequence in Python?

The equation for the Fibonacci sequence in Python is: 

F(n) = F(n-1) + F(n-2) using recursion or iterative methods.


How to calculate Fibonacci Series In Python?

There are various methods to calculate fibonacci series in Python such as – 

  • Loops
  • Recursion
  • Matrix Exponentiation

Aniket

Aniket

Leave a Reply

Your email address will not be published. Required fields are marked *

Blogs You May Like

Get in touch to claim Best Available Discounts.

If You Are Looking for Job Assistance Please Fill Up the Form.

× How can I help you?