Fibonacci Python Program

Today in this tutorial, we will learn how to write the Fibonacci python program. In python programming, the Fibonacci series can be implemented in many ways like memorization or by using the lru_cache method.

fibonacci python program

First of all, you should know about the Fibonacci series. In the Fibonacci python program, the series is produced by just adding the two numbers from the left side to produce the next number.

Fibonacci Python Program

In Fibonacci series, the first two number is added to produce the next number, As you can see below.

  • 0+ 1 = 1
  • 1 + 1 = 2
  • 1 + 2 = 3
  • 2 + 3 = 5
  • 3 + 5 = 8 and so on.

Fibonacci Python Program

1) Fibonacci Python Program To Print Fibonacci Series Using Loop

Fibonacci Python Series

Code

#Take a input from user for fibbonacci series
n = int(input('Enter the number for Fibonacci Series : '))

#initialize a and b.
a = 0
b = 1

#if condition for n = 1
if n == 1:
  print(a)

#And when the value of n > 1
else:
  print(a, end = ' ')
  print(b, end = ' ')

#Loop for producing the Fibonacci Series
for i in range(2, n):
  c = a+b
  a = b
  b = c
  print(c , end=' ')

In this Fibonacci Python program, first of all, take input from the user for the Fibonacci number. After that initialize the two variables (a, b) with values 0 and 1. And then print the value of a and b when the value of n is equal to 1 then print the value of a, and if the value of n is greater than 1, then print the value of both a and b. At last, for-loop conditions for producing the Fibonacci series by just swaiping the value of a = b, b = c, and printing the value of c continuously.

2) Fibonacci Python Program To Print Nth Fibonacci Number Using Recursion:

Fibonacci python program by using recursion

Code

#Defining function of fibonacci fib
def fib(n):
  #returning the value if n=1 return 0.
  if n == 1:
    return 0
  #returning the value if n=2 or 3 return 1.
  if n==2 or n==3:
    return 1
  #Using recursion for fibonacci number.
  return fib(n-1)+fib(n-2)

#take a input of n from the user.
n = int(input("Enter to print nth number : "))
#printing the nth fibonacci snumber
print(fib(n))

In this Fibonacci Python program, This code is about to find the nth number of the Fibonacci series. We are using Recursion to find the nth number.

3) Fibonacci Python Program To Print Nth Term By Using Memorization

i) Normal Method for Memorization

Fibonacci Python Program by memorization

Code

#Use the cache method for memorization
fibonacci_cache = {}

#define fibonacci
def fibonacci(n):
    # if we have cached value then return it
    if n in fibonacci_cache:
        return fibonacci_cache[n]
    #compute for the nth term
    if n==1:
        value = 0
    elif n==2:
        value = 1
    elif n>2:
        value = fibonacci(n-1) + fibonacci(n-2)
    
    #cache the value and return it
    fibonacci_cache[n] = value
    return value
    
for n in range(1, 11):
    print(fibonacci(n), end=" ")

In the above Fibonacci program, Memorization is used in a function to return the Nth term of the Fibonacci sequence. By using memorization the program is fast and solid.

ii) By Using rlu_cache Method

Fibonacci Python Program by using relu_cache memorization

Code

from functools import lru_cache
@lru_cache(maxsize = 1000)

def fib(n):
  #check the number is positive number
  if type(n) != int:
    raise TypeError('n must be a positive intiger')
  if n<1:
    raise ValueError('n must be a positive intiger')

    #compute the nth term
  if n==1:
    return 0
  elif n==2:
    return 1
  elif n>2:
    return fib(n-1) + fib(n-2)
    
for n in range(1, 11):
  print(fib(n), end=" ")

In the above Fibonacci Python Program, Memorization is used by the lru_cache method. In this, the least recently used (LRU) method is used for producing the Fibonacci sequence.


Video Tutorial for Fibonacci Python program

Learn More Trick in Python Programming

FAQ for Fibonacci Python Program

What is Fibonacci Sequence?

0 1 1 2 3 5 8 13 21 34 … are the sequence of Fibonacci. This Sequence is produced by just adding two below numbers for the next number.

Ways for Fibonacci Python Program?

In Python Programming Language, The Fibonacci sequence program can be written in many ways like using the lru_cache method, memorization, recursion, and normal method.

2 thoughts on “Fibonacci Python Program”

  1. I believe that is one of the so much significant info for me.
    And i am happy studying your article. However want to observation on some basic issues, The site style is ideal, the articles is
    truly nice : D. Excellent task, cheers

    Reply

Leave a Comment

Share via
Copy link
Powered by Social Snap