
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python Program for n-th Fibonacci number
In this article, we will compute the nth Fibonacci number.
A Fibonacci number is defined by the recurrence relation given below −
Fn = Fn-1 + Fn-2
With F0= 0 and F1 = 1.
First, few Fibonacci numbers are
0,1,1,2,3,5,8,13,..................
We can compute the Fibonacci numbers using the method of recursion and dynamic programming.
Now let’s see the implementation in the form of Python script
Approach 1: Recursion Method
Example
#recursive approach def Fibonacci(n): if n<0: print("Fibbonacci can't be computed") # First Fibonacci number elif n==1: return 0 # Second Fibonacci number elif n==2: return 1 else: return Fibonacci(n-1)+Fibonacci(n-2) # main n=10 print(Fibonacci(n))
Output
34
The scope of all the variables declared is shown below.
Approach 2: Dynamic Programming Method
Example
#dynamic approach Fib_Array = [0,1] def fibonacci(n): if n<0: print("Fibbonacci can't be computed") elif n<=len(Fib_Array): return Fib_Array[n-1] else: temp = fibonacci(n-1)+fibonacci(n-2) Fib_Array.append(temp) return temp # Driver Program n=10 print(fibonacci(n))
Output
34
The scope of all the variables declared is shown below
Conclusion
In this article, we learned about the computation of nth Fibonacci number using recursion and dynamic programming approach.
- Related Articles
- Java Program for n-th Fibonacci number
- N-th Fibonacci number in Python Program
- C/C++ Program for the n-th Fibonacci number?
- C Program for n-th even number
- C Program for n-th odd number
- Python Program for Fibonacci numbers
- Python program for removing n-th character from a string?
- Python Program for nth multiple of a number in Fibonacci Series
- An efficient way to check whether n-th Fibonacci number is multiple of 10?
- Python Program for How to check if a given number is a Fibonacci number?
- Program to find Nth Fibonacci Number in Python
- Program to find minimum number of Fibonacci numbers to add up to n in Python?
- Java Program for check if a given number is Fibonacci number?
- Python Program for Find sum of Series with the n-th term as n^2 – (n-1)^2
- C program to find Fibonacci series for a given number

Advertisements