
- 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
N-th Fibonacci number in Python Program
In this article, we will compute nth Fibonacci number.
A fibbonacci number is defined by the recurrance relation given below:
Fn = Fn-1 + Fn-2
With F0 = 0 and F1 = 1.
First few fibbonacci 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 computation of nth Fibonacci number using recursion and dynamic programming approach.
- Related Articles
- Python Program for n-th Fibonacci number
- Java Program for n-th Fibonacci number
- C/C++ Program for the n-th Fibonacci number?
- Program to find Nth Fibonacci Number in Python
- An efficient way to check whether n-th Fibonacci number is multiple of 10?
- C Program for n-th even number
- C Program for n-th odd number
- Program to find minimum number of Fibonacci numbers to add up to n in Python?
- N-th Tribonacci Number in C++
- N-th polite number in C++
- Python Program for nth multiple of a number in Fibonacci Series
- Program to check given number is a Fibonacci term in Python
- Java Program to Find Even Sum of Fibonacci Series till number N
- Swift Program to Find Sum of Even Fibonacci Terms Till number N
- How to check if a given number is a Fibonacci number in Python Program ?

Advertisements