Python Program to Find Fibonacci Series
You can find the Fibonacci series in Python by using a loop that starts with
0 and 1 and adds the last two numbers to get the next one, like this: for i in range(n): print(a, end=' '); a, b = b, a + b.Examples
Input5
Output0 1 1 2 3
Input1
Output0
Input0
Output
How to Think About It
To find the Fibonacci series, start with the first two numbers 0 and 1. Then, keep adding the last two numbers to get the next number in the series. Repeat this process until you reach the desired length.
Algorithm
1
Get the number of terms to generate, n.2
Initialize two variables with 0 and 1 as the first two Fibonacci numbers.3
Loop from 0 to n-1.4
In each loop, print the current number and update the two variables to the next two Fibonacci numbers.5
Stop when n numbers are printed.Code
python
n = int(input('Enter number of terms: ')) a, b = 0, 1 for _ in range(n): print(a, end=' ') a, b = b, a + b
Output
Enter number of terms: 5
0 1 1 2 3
Dry Run
Let's trace the input 5 through the code
1
Initialize
a = 0, b = 1
2
Iteration 1
Print 0, update a=1, b=1
3
Iteration 2
Print 1, update a=1, b=2
4
Iteration 3
Print 1, update a=2, b=3
5
Iteration 4
Print 2, update a=3, b=5
6
Iteration 5
Print 3, update a=5, b=8
| Iteration | a (printed) | b (next) |
|---|---|---|
| 1 | 0 | 1 |
| 2 | 1 | 1 |
| 3 | 1 | 2 |
| 4 | 2 | 3 |
| 5 | 3 | 5 |
Why This Works
Step 1: Start with first two numbers
The Fibonacci series always begins with 0 and 1, so we set a = 0 and b = 1.
Step 2: Print current number
In each loop, we print the current number a which is the next number in the series.
Step 3: Update numbers
We update a and b to the next two Fibonacci numbers by setting a = b and b = a + b (using the old values).
Alternative Approaches
Using recursion
python
def fib(n): if n <= 1: return n else: return fib(n-1) + fib(n-2) n = int(input('Enter number of terms: ')) for i in range(n): print(fib(i), end=' ')
Simple but inefficient for large n due to repeated calculations.
Using dynamic programming (memoization)
python
def fib(n, memo={}): if n in memo: return memo[n] if n <= 1: memo[n] = n else: memo[n] = fib(n-1, memo) + fib(n-2, memo) return memo[n] n = int(input('Enter number of terms: ')) for i in range(n): print(fib(i), end=' ')
Faster than plain recursion by storing results but uses extra memory.
Complexity: O(n) time, O(1) space
Time Complexity
The loop runs n times, so the time grows linearly with n.
Space Complexity
Only a few variables are used, so space stays constant regardless of n.
Which Approach is Fastest?
The iterative loop approach is fastest and uses least memory compared to recursion or memoization.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Iterative loop | O(n) | O(1) | Most efficient for large n |
| Recursion | O(2^n) | O(n) | Simple but slow, small n only |
| Memoization | O(n) | O(n) | Faster recursion with extra memory |
Use a loop with two variables to generate Fibonacci numbers efficiently without extra memory.
Beginners often forget to update both variables simultaneously, causing incorrect sequences.