- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
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
#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))
The scope of all the variables declared is shown below.
Approach 2: Dynamic Programming Method
#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))
The scope of all the variables declared is shown below
In this article, we learned about the computation of nth Fibonacci number using recursion and dynamic programming approach.
- 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?
- An efficient way to check whether n-th Fibonacci number is multiple of 10?
- Python Program for nth multiple of a number in Fibonacci Series
- N-th Tribonacci Number in C++
- N-th polite number in C++
- Python Program for How to check if a given number is a Fibonacci number?
- Java Program for check if a given number is Fibonacci number?
- Java program for removing n-th character from a string
- C Program for N-th term of Geometric Progression series