Table of contents | |
Introduction | |
Generating the Fibonacci Series | |
Python | |
C++ | |
Applications of the Fibonacci Series |
The Fibonacci series is a sequence of numbers in which each number (after the first two) is the sum of the two preceding ones. The sequence usually starts with 0 and 1.
The formula to calculate the nth Fibonacci number is:
F(n) = F(n - 1) _ F(n - 2)
Where initial conditions are:
F(0) = 0
F(1) = 1
Here is how the series progresses:
0,1,1,2,3,5,8,13,21,34..
The Fibonacci series can be generated using various methods, such as recursion, iteration, and dynamic programming.
Below are examples in Python for each method:
Iterative approach:
def fibonacci_iterative(n):
fib_sequence = [0, 1]
while len(fib_sequence) <= n:
fib_sequence.append(fib_sequence[-1] + fib_sequence[-2])
return fib_sequence[:n+1]
n = 10
print(fibonacci_iterative(n))
Recursive Approach:
def fibonacci_recursive(n):
if n <= 0:
return 0
elif n == 1:
return 1
else:
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2)
n = 10
fib_sequence = [fibonacci_recursive(i) for i in range(n)]
print(fib_sequence)
Iterative Approach:
#include <iostream>
#include <vector>
std::vector<int> fibonacci_iterative(int n) {
std::vector<int> fib_sequence = {0, 1};
for (int i = 2; i <= n; ++i) {
fib_sequence.push_back(fib_sequence[i-1] + fib_sequence[i-2]);
}
return fib_sequence;
}
int main() {
int n = 10;
std::vector<int> result = fibonacci_iterative(n);
for (int num : result) {
std::cout << num << " ";
}
return 0;
}
Recursive Approach:
#include <iostream>
int fibonacci_recursive(int n) {
if (n <= 0) {
return 0;
} else if (n == 1) {
return 1;
} else {
return fibonacci_recursive(n-1) + fibonacci_recursive(n-2);
}
}
int main() {
int n = 10;
for (int i = 0; i < n; ++i) {
std::cout << fibonacci_recursive(i) << " ";
}
return 0;
}
119 docs|30 tests
|
1. What is the Fibonacci series and how is it generated? |
2. How is the Fibonacci series used in Computer Science Engineering (CSE)? |
3. What are some common applications of the Fibonacci series? |
4. How can the Fibonacci series be implemented in programming languages like Python or Java? |
5. What are some real-world examples where the Fibonacci series is used in practical applications? |
|
Explore Courses for Computer Science Engineering (CSE) exam
|