Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Fibonacci Series in Python using Recursion

In Python or other programming languages, Fibonacci series is a list of infinite numbers, each of which is the sum of past two numbers (starting with 0).

For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, . . . .

You will notice in the above example, the first two terms are fixed i.e. 0 and 1. All other numbers are obtained by adding the preceding two numbers.

It means to say the nth number is the sum of (n-1)th and (n-2)th number. We can formulate this sequence of numbers as follows:

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

In the above equation, the first two numbers are fixed, i.e. F(0) = 0 and F(1) = 1.

For example:

  • F(2) = F(2 – 1) + F(2 – 2) = F(1) + F(0) = 1 + 0 = 1
  • F(3) = F(3 – 1) + F(3 – 2) = F(2) + F(1) = 1 + 1 = 2
  • F(4) = F(4 – 1) + F(4 – 2) = F(3) + F(2) = 2 + 1 = 3 and so on.

There are two solutions to determine the Fibonacci Series. They are:

  • Recursive solution
  • Iterative solution

Let’s first understand the recursive method.

Recursive Method: Fibonacci series upto nth Terms


Let’s write a program in Python to print the Fibonacci series or sequence upto nth terms using the recursion method.

Program code:

# Python program to print the Fibonacci series upto n terms using recursion.
# Create a function to calculate the Fibonacci series (recursive).
# Returns an integer value.
def recur_fibo(n):
    if n == 0: # base condition.
        return 0
    elif n == 1: # base condition.
        return 1
    else:
        value = recur_fibo(n-1) + recur_fibo(n-2) # recursive call.
        return value

# Main part of program.
# Take the input from the user.
nterms = int(input('Enter the number of terms until you want to find Fibonacci series? '))

# checking the entered number of terms is valid or not.
if nterms 
Output:
      Enter the number of terms until you want to find a Fibonacci series? 10
      Fibonacci sequence:
      0 1 1 2 3 5 8 13 21 34 

Explanation:

1. In the above program, we have taken the number of terms 10 as a user and store it in a variable nterms. Then, we have checked the number of terms is valid or not.

2. We have passed the number as an argument to a recursive function named recur_fibo.

3. Inside the recursive function, we have defined two base conditions that return 0 and 1 if the number is equal to 0 and 1.

4. Otherwise, we have called the function recursively with the argument as the number minus 1 added to the function called recursively with the argument as the number minus 2 and stored the result in a variable value. Then, we have returned the value to the caller.

5. We have used a for loop and printed the returned value, which is the Fibonacci series.

Iterative Method: Fibonacci series


Let’s write a program in Python for Fibonacci series using loop. This is an iterative solution.

Program code:

# Python program to print the Fibonacci series upto n terms using iterative.
def itr_fibo(n):
    a = 0
    b = 1
    for i in range(n):
        temp = a
        a = b
        b = temp + b
    return a

# Take the input from the user.
nterms = int(input('Enter the number of terms: '))

# checking the entered number of terms is valid or not.
if nterms 
Output:
      Enter the number of terms: 11
      Fibonacci sequence:
      0 1 1 2 3 5 8 13 21 34 55

Python Program to Calculate Sum of Fibonacci Series using Recursive Method


Let’s write a program in Python to calculate the sum of Fibonacci series of a number using recursive method.

Program code:

# Python program to print the sum of Fibonacci series upto 10th terms.
def sumFibo(num):
    if num 
Output:
      Sum of Fibonacci numbers: 610

In this tutorial, you have learned about how to print the Fibonacci series using recursion in Python with example. Hope that you will have also understood the iterative method to print Fibonacci series and practiced all programs.
Thanks for reading!!!

⇐ Prev Next ⇒

The post Fibonacci Series in Python using Recursion appeared first on Scientech Easy.



This post first appeared on Scientech Easy, please read the originial post: here

Share the post

Fibonacci Series in Python using Recursion

×

Subscribe to Scientech Easy

Get updates delivered right to your inbox!

Thank you for your subscription

×