C++ Program For Fibonacci Numbers
The Fibonacci series is the sequence where each number is the sum of the previous two numbers. The first two numbers of the Fibonacci series are 0 and 1 and are used to generate the whole series.
In this article, we will learn how to find the nth Fibonacci number in C++.
Examples
Input: 5
Output: 5
Explanation: As 5 is the 5th Fibonacci number of series 0, 1, 1, 2, 3, 5, 8, 13…. (0-based indexing)Input: 1
Output: 1
Explanation: As 1 is the 1st Fibonacci number of series 0, 1, 1, 2, 3, 5, 8, 13…. (0-based indexing)
Following are the different ways to find the given term of the Fibonacci series in C++:
Table of Content
Using Recursion
The simplest way to find the nth Fibonacci number is by using recursion. In this approach, we define a function that returns the nth Fibonacci number based on the relation: F(n) = F(n-1) + F(n-2), where the base cases are F(0) = 0 and F(1) = 1. If the input value is 0 or 1, the function directly returns the input. Otherwise, it recursively calls itself with n-1 and n-2 to compute the result.
Code Implementation
// C++ Program to find the nth fibonacci number using
// recursion
#include <bits/stdc++.h>
using namespace std;
int fib(int n) {
// If n is 1 or 0, then return n, works for 0th
// and 1st terms
if (n <= 1)
return n;
// Recurrence relation to find the rest of the
// terms
return fib(n - 1) + fib(n - 2);
}
int main() {
int n = 5;
// Finding nth term
cout << fib(n);
return 0;
}
Output
5
Time Complexity: O (2n), where n is the number of term to find.
Auxiliary Space: O (n)
Optimization of Recursion Method
The complexity of the above method is very high as it is calculating all the previous fibonacci numbers for each recursive call. Due to this, previous fibonacci numbers are being calculated multiple times. We can avoid this by passing the previous numbers as parameters to the recursive function.
// C++ Program to print the nth Fibonacci number using
// tail recursion
#include <iostream>
using namespace std;
// Helper function to find the nth fibonacci number
// using recursion
int fibHelper(int n, int prev2, int prev1) {
// When n reaches 0, return prev2
if (n == 0) {
return prev2;
}
// When n reaches 1, return prev1
if (n == 1) {
return prev1;
}
// Recursive call with updated parameters to find
// rest of the fibonacci numbers
return fibHelper(n - 1, prev1, prev2 + prev1);
}
int fib(int n) {
// Calling recursive function
return fibHelper(n, 0, 1);
}
int main() {
int n = 5;
// Finding the nth Fibonacci number
cout << fib(n);
return 0;
}
Output
5
Time Complexity: O (n), where n is the number of term to find.
Auxiliary Space: O (n)
Using Loops
We can also find nth Fibonacci number using loops. In this approach, we keep two variables to keep the track of the last two Fibonacci numbers. We run a loop that runs in which we add the last two number variables to find the current term and then update these variables with current term and move to find the next term. We do that till we find the nth term.
Code Implementation
// C++ Program to find the nth fibonacci number using
// loops
#include <bits/stdc++.h>
using namespace std;
int fib(int n) {
// For 0th and 1st term
if (n <= 1)
return n;
// Variable to store the last two terms
int prev1 = 1, prev2 = 0;
// Variable that stores the current fibonacci term
int curr;
// Calculating the next fibonacci number by using
// the previous two number
for (int i = 2; i <= n; i++) {
curr = prev1 + prev2;
prev2 = prev1;
prev1 = curr;
}
return curr;
}
int main() {
int n = 5;
// Finding the nth fibonacci number
cout << fib(n);
return 0;
}
Output
5
Time Complexity: O(n), where n is the number of term to find.
Auxiliary Space: O(1)
Using Matrix Exponentiation
In this approach, the Fibonacci series is represented as a transformation matrix using the relation for Fibonacci Sequence is F(n) = F(n – 1) + F(n – 2) starting with F(0) = 0 and F(1) = 1 .It allows us to calculate the nth term efficiently by raising the matrix to the (n-1)th power. We use matrix exponentiation method which is used to calculate a matrix raised to a power efficiently.
Code Implementation
// C++ Program to find the nth fibonacci number using
// loops
#include <bits/stdc++.h>
using namespace std;
// Function to multiply 2 * 2 matrix
void multiply(vector<vector<int>> &m1,
vector<vector<int>> &m2) {
// Matrix to store result
vector<vector<int>> res(2, vector<int>(2));
res[0][0] = m1[0][0] * m2[0][0]
+ m1[0][1] * m2[1][0];
res[0][1] = m1[0][0] * m2[0][1]
+ m1[0][1] * m2[1][1];
res[1][0] = m1[1][0] * m2[0][0]
+ m1[1][1] * m2[1][0];
res[1][1] = m1[1][0] * m2[0][1]
+ m1[1][1] * m2[1][1];
// Copying Multiplied matrix to m1
m1[0][0] = res[0][0];
m1[0][1] = res[0][1];
m1[1][0] = res[1][0];
m1[1][1] = res[1][1];
}
// Calculating the power(mat,n) in log n time
// using matrix exponentiation
void power(vector<vector<int>> &m, int n) {
// Base case
if(n == 0 || n == 1)
return;
// Identity matrix
vector<vector<int>> i = {{1, 1},
{1, 0}};
power(m, n / 2);
multiply(m, m);
if (n % 2 != 0)
multiply(m, i);
}
int fib(int n) {
// For 0th and 1st term
if (n < 2)
return n;
// Representing fibonacci series as matrix
vector<vector<int>> m = {{1, 1},
{1, 0}};
// Finding (n - 1)th power of matrix m
power(m, n - 1);
// Returning nth fibonacci number
return m[0][0];
}
int main() {
int n = 5;
// Finding nth fibonacci number
cout << fib(n);
return 0;
}
Output
5
Time Complexity: O(log n), where n is the number of term to find.
Auxiliary Space: O(log n)