The Fibonacci Series is one of the most common and fundamental concepts in Data Structures and Algorithms (DSA).
It forms the basis for understanding recursion, dynamic programming, and algorithm optimization.
π’ What is the Fibonacci Series?
The Fibonacci series is a sequence of numbers where:
F(n) = F(n-1) + F(n-2)
with the first two numbers defined as:
F(0) = 0, F(1) = 1
So, the series looks like:
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
Each number is the sum of the two preceding numbers.
π‘ Real-World Applications
You may be surprised, but the Fibonacci sequence appears in many real-world scenarios, such as:
π» Nature: Patterns in flowers and leaves
π° Finance: Predicting stock market movements
𧬠Biology: DNA and population growth models
π» Computer Science: Algorithm design and dynamic programming problems
π§ Approach 1. Iterative Method
The iterative approach is more efficient and simple to implement.
β
Algorithm Steps
Initialize first two numbers as 0
and 1
.
Loop from 2 to n
.
Calculate the next number as the sum of the previous two.
Print each number as you go.
π» C Program Example
#include <stdio.h>
int main() {
int n, first = 0, second = 1, next, i;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for(i = 0; i < n; i++) {
if(i <= 1)
next = i;
else {
next = first + second;
first = second;
second = next;
}
printf("%d ", next);
}
return 0;
}
π Approach 2. Recursive Method
The recursive approach follows the direct mathematical definition of Fibonacci numbers.
However, it is less efficient because it performs repeated calculations.
π» C Program Example
#include <stdio.h>
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
printf("Fibonacci Series: ");
for(int i = 0; i < n; i++) {
printf("%d ", fibonacci(i));
}
return 0;
}
βοΈ Optimized Approach (Using Dynamic Programming)
To improve performance, store results of previous calculations (called memoization).
π» C Program Example
#include <stdio.h>
int fibonacci(int n, int memo[]) {
if (memo[n] != -1)
return memo[n];
if (n <= 1)
return n;
memo[n] = fibonacci(n - 1, memo) + fibonacci(n - 2, memo);
return memo[n];
}
int main() {
int n;
printf("Enter the number of terms: ");
scanf("%d", &n);
int memo[n + 1];
for(int i = 0; i <= n; i++)
memo[i] = -1;
printf("Fibonacci Series: ");
for(int i = 0; i < n; i++)
printf("%d ", fibonacci(i, memo));
return 0;
}
π― Comparison Table
π§© Approach | β± Time Complexity | πΎ Space Complexity | π Efficiency |
---|
Iterative | O(n) | O(1) | β
High |
Recursive | O(2βΏ) | O(n) | β Low |
Dynamic Programming | O(n) | O(n) | β‘ Very High |
π§© Key Takeaways
Fibonacci series demonstrates recursion and iteration concepts clearly.
Iterative and dynamic programming methods are more efficient.
Always consider time complexity when implementing recursive solutions.
π¬ Conclusion
The Fibonacci series is not just a simple number patternβitβs a gateway to understanding recursion, optimization, and dynamic programming.
Whether youβre preparing for coding interviews or improving your DSA skills, mastering this concept gives you a solid foundation for solving more complex problems.