
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Fibonacci Numbers Using Matrix Exponentiation in C++
The Fibonacci numbers, commonly denoted as F(n) forms a sequence, called the Fibonacci sequence. In Fibonacci series, each number is the sum of the two previous two numbers, starting from 0 and 1. It is represented as F(n) = F(n-1) + F(n-2). The matrix exponentiation method is used to calculate the powers of matrix efficiently with better time complexity.
In this article, we provide a value of n. This n is the value up to which we want to find the Fibonacci numbers using the matrix exponentiation method in C++. Here is an example of the Fibonacci series up to n=6:
Input: n = 6 Output: Fibonacci series up to 6 terms: 1 1 2 3 5 8
Here is the explanation of the above example:
F(n) = F(n-1) + F(n-2) F(0) = 0, F(1) = 1 F(2) = F(1) + F(0) = 1 + 0 = 1 F(3) = F(2) + F(1) = 1 + 1 = 2 F(4) = F(3) + F(2) = 2 + 1 = 3 F(5) = F(4) + F(3) = 3 + 2 = 5 F(6) = F(5) + F(4) = 5 + 3 = 8 => Fibonacci series up to 6 terms: 1 1 2 3 5 8
Calculating Fibonacci Numbers Using Matrix Exponentiation
We will use the following steps to find the Fibonacci series using the matrix exponentiation. The steps are as follows:
- We have used the multiply() function to multiply the starting matrix 'F[][]' and the base Fibonacci matrix 'M[][]'. This performs the in-place matrix multiplication and stores the result back in matrix F[][].
- The power() function calculates the power of the matrix 'F[][]'. In this, the power 'n' represents the number up to which we want to find the Fibonacci series.
- In this function, first we have defined the base case for the recursive function power() i.e. for n = 0 and 1.
- Then we have recursively called the function power(). We have applied the divide and conquer method here and depending on the n whether it is even or odd, we decide to either square the F[][] matrix or to multiply the F[][] matrix with M[][] matrix.
- The Fibonacci_matrix() function calculates the Fibonacci series up to 'n'. First we have defined the starting value of the series using if statement. It returns 0 for the 0th Fibonacci number.
- Then we have defined an F[][] matrix storing the starting matrix and called the power() function. The power function then makes use of multiply() function to update this F[][] matrix.
- At the end, leftmost element of F[][] matrix is returned which is the Fibonacci value for the nth element i.e F(n). In the main() function, using a for loop we have printed the complete Fibonacci series up to n.
Fibonacci Numbers Using Matrix Exponentiation in C++
Here is the C++ code implementation of the above steps to find the Fibonacci series using matrix exponentiation up to n.
#include <iostream> using namespace std; void multiply(int F[2][2], int M[2][2]) { int a = F[0][0] * M[0][0] + F[0][1] * M[1][0]; int b = F[0][0] * M[0][1] + F[0][1] * M[1][1]; int c = F[1][0] * M[0][0] + F[1][1] * M[1][0]; int d = F[1][0] * M[0][1] + F[1][1] * M[1][1]; F[0][0] = a; F[0][1] = b; F[1][0] = c; F[1][1] = d; } void power(int F[2][2], int n) { if (n == 0 || n == 1) return; int M[2][2] = {{1, 1}, {1, 0}}; power(F, n / 2); multiply(F, F); if (n % 2 != 0) multiply(F, M); } int Fibonacci_matrix(int n) { if (n == 0) return 0; int F[2][2] = {{1, 1}, {1, 0}}; power(F, n - 1); return F[0][0]; } int main() { int n = 10; cout << "Fibonacci series up to " << n << " terms:\n"; for (int i = 1; i <= n; ++i) { cout << Fibonacci_matrix(i) << " "; } cout << endl; return 0; }
The output of the above code is:
Fibonacci series up to 10 terms: 1 1 2 3 5 8 13 21 34 55
Complexity of Fibonacci Using Matrix Exponentiation
The time complexity of the Fibonacci series using matrix exponentiation is O(log n). The space complexity is O(1).