# 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,

N = 4

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

Live Demo

#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

Live Demo

#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