Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program for n-th Fibonacci number
In this article, we will compute the nth Fibonacci number using different approaches in Python.
A Fibonacci number is defined by the recurrence relation given below −
Fn = Fn-1 + Fn-2
With F0 = 0 and F1 = 1.
The first few Fibonacci numbers are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
We can compute the Fibonacci numbers using recursion and dynamic programming methods. Let's explore both approaches with their implementations.
Using Recursion Method
The recursive approach directly implements the mathematical definition. However, it has exponential time complexity due to repeated calculations ?
def fibonacci_recursive(n):
if n < 0:
print("Fibonacci can't be computed for negative numbers")
return None
elif n == 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
# Calculate 10th Fibonacci number
n = 10
result = fibonacci_recursive(n)
print(f"The {n}th Fibonacci number is: {result}")
The 10th Fibonacci number is: 55
Using Dynamic Programming Method
The dynamic programming approach stores previously computed values to avoid redundant calculations, achieving linear time complexity ?
def fibonacci_dp(n):
if n < 0:
print("Fibonacci can't be computed for negative numbers")
return None
elif n == 0:
return 0
elif n == 1:
return 1
# Initialize array with base cases
fib_array = [0, 1]
# Build up from bottom
for i in range(2, n + 1):
fib_array.append(fib_array[i-1] + fib_array[i-2])
return fib_array[n]
# Calculate 10th Fibonacci number
n = 10
result = fibonacci_dp(n)
print(f"The {n}th Fibonacci number is: {result}")
print(f"Fibonacci sequence up to {n}th term:")
# Show the sequence
for i in range(n + 1):
print(f"F({i}) = {fibonacci_dp(i)}")
The 10th Fibonacci number is: 55 Fibonacci sequence up to 10th term: F(0) = 0 F(1) = 1 F(2) = 1 F(3) = 2 F(4) = 3 F(5) = 5 F(6) = 8 F(7) = 13 F(8) = 21 F(9) = 34 F(10) = 55
Iterative Approach (Most Efficient)
For optimal space efficiency, we can use an iterative approach that only stores the last two values ?
def fibonacci_iterative(n):
if n < 0:
print("Fibonacci can't be computed for negative numbers")
return None
elif n == 0:
return 0
elif n == 1:
return 1
# Only keep track of last two values
prev2, prev1 = 0, 1
for i in range(2, n + 1):
current = prev1 + prev2
prev2, prev1 = prev1, current
return prev1
# Calculate 10th Fibonacci number
n = 10
result = fibonacci_iterative(n)
print(f"The {n}th Fibonacci number is: {result}")
The 10th Fibonacci number is: 55
Comparison of Approaches
| Method | Time Complexity | Space Complexity | Best For |
|---|---|---|---|
| Recursive | O(2^n) | O(n) | Educational purposes |
| Dynamic Programming | O(n) | O(n) | When you need the entire sequence |
| Iterative | O(n) | O(1) | Single value, memory efficient |
Conclusion
The recursive approach is intuitive but inefficient for large n due to exponential time complexity. The dynamic programming method is much faster with O(n) time complexity, while the iterative approach provides the best space efficiency with O(1) space complexity. Choose the iterative method for production code when computing single Fibonacci numbers.
