- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 Nth term (A matrix exponentiation example) in C++
In this problem, we are given an integer N and a recursive function that given Nth term as a function of other terms. Our task is to create a program to Find Nth term (A matrix exponentiation example).
The function is
T(n) = 2*( T(n-1) ) + 3*( T(n-2) ) Initial values are T(0) = 1 , T(1) = 1
Let’s take an example to understand the problem,
Input
N = 4
Output
41
Explanation
T(4) = 2* (T(3)) + 3*(T(2)) T(4) = 2* ( 2*(T(2)) + 3*(T(1)) ) + 3*( 2* (T(1)) + 3*(T(0)) ) T(4) = 2*( 2*(2* (T(1)) + 3*(T(0))) + 3*(1) ) + 3*(2*(1) + 3*(1)) T(4) = 2*(2 * (2 *(1) + 3*(1) )) + 3 ) + 3 * (5) T(4) = 2*(2 * (2 + 3) + 3) + 15 T(4) = 2*(2 * (5) + 3) + 15 T(4) = 2*(10 + 3) + 15 T(4) = 2*(13) + 15 = 26 + 15 = 41
Solution Approach
A simple approach to solve the problem is using recursion or iteration. We can find nth term as a recursive call to previous terms and using initial values the result can be found.
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; long calcNthTerm(long n) { if(n == 0 || n == 1) return 1; return ( ( 2*(calcNthTerm(n-1)) ) + ( 3*(calcNthTerm(n-2)) ) ); } int main() { long n = 5; cout<<n<<"th term of found using matrix exponentiation is "<<calcNthTerm(n); return 0; }
Output
5th term of found using matrix exponentiation is 121
Efficient approach
An efficient approach to solve the problem is using the concept of matrix exponentiation. In this method, we will use a transform matrix to find Nth term.
For this we need to find the transform matrix. The matrix is dependent on the number of dependent terms which happens to be 2 here. And the initial values which are T(0) = 1, T(1)= 1.
The transform matrix is of the size k*k. Which when multiplied with the initial matrix of size k*1, returns the next term.
Here are the values,
initial matrix =
$$\begin{bmatrix}1 \0 \end{bmatrix}$$
Transform matrix =
$$\begin{bmatrix}2&3 \1&0 \end{bmatrix}$$
The values of Tn is given as TM(n-1)*IM
$$\begin{bmatrix}2&3 \1&0 \end{bmatrix}^{n-1}*\begin{bmatrix}2 \3 \end{bmatrix}$$
Program to illustrate the working of our solution,
Example
#include <iostream> using namespace std; #define MOD 1000000009 long calcNthTerm(long n) { if (n <= 1) return 1; n--; long resultantMat[2][2] = { 1, 0, 0, 1 }; long transMat[2][2] = { 2, 3, 1, 0 }; while (n) { long tempMat[2][2]; if (n & 1) { tempMat[0][0] = (resultantMat[0][0] * transMat[0][0] + resultantMat[0][1] * transMat[1][0]) % MOD; tempMat[0][1] = (resultantMat[0][0] * transMat[0][1] + resultantMat[0][1] * transMat[1][1]) % MOD; tempMat[1][0] = (resultantMat[1][0] * transMat[0][0] + resultantMat[1][1] * transMat[1][0]) % MOD; tempMat[1][1] = (resultantMat[1][0] * transMat[0][1] + resultantMat[1][1] * transMat[1][1]) % MOD; resultantMat[0][0] = tempMat[0][0]; resultantMat[0][1] = tempMat[0][1]; resultantMat[1][0] = tempMat[1][0]; resultantMat[1][1] = tempMat[1][1]; } n = n / 2; tempMat[0][0] = (transMat[0][0] * transMat[0][0] + transMat[0][1] * transMat[1][0]) % MOD; tempMat[0][1] = (transMat[0][0] * transMat[0][1] + transMat[0][1] * transMat[1][1]) % MOD; tempMat[1][0] = (transMat[1][0] * transMat[0][0] + transMat[1][1] * transMat[1][0]) % MOD; tempMat[1][1] = (transMat[1][0] * transMat[0][1] + transMat[1][1] * transMat[1][1]) % MOD; transMat[0][0] = tempMat[0][0]; transMat[0][1] = tempMat[0][1]; transMat[1][0] = tempMat[1][0]; transMat[1][1] = tempMat[1][1]; } return (resultantMat[0][0] * 1 + resultantMat[0][1] * 1) % MOD; } int main() { long n = 5; cout<<n<<"th term of found using matrix exponentiation is "<<calcNthTerm(n); return 0; }
Output
5th term of found using matrix exponentiation is 121
- Related Articles
- C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
- Find nth term of a given recurrence relation in C++
- Program to find Nth term divisible by a or b in C++
- Find nth term of the Dragon Curve Sequence in C++
- C program to find nth term of given recurrence relation
- Find nth term of a given recurrence relation in Python
- Program to find nth Fibonacci term in Python
- Find the Nth term of the series 9, 45, 243,1377…in C++
- The nth term of a sequence is $(2n-3)$, find its fifteenth term.
- Find Pth term of a GP if Mth and Nth terms are given in C++
- Program to find nth term of a sequence which are divisible by a, b, c in Python
- Find the nth term of the series 0, 8, 64, 216, 512,... in C++
- Program to find nth smallest number from a given matrix in Python
- C++ program to find Nth term of series 1, 4, 15, 72, 420…
- C++ program to find Nth term of the series 1, 5, 32, 288 …
