# 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

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 = { 1, 0, 0, 1 };
long transMat = { 2, 3, 1, 0 };
while (n) {
long tempMat;
if (n & 1) {
tempMat = (resultantMat * transMat +
resultantMat * transMat) % MOD;
tempMat = (resultantMat * transMat +
resultantMat * transMat) % MOD;
tempMat = (resultantMat * transMat +
resultantMat * transMat) % MOD;
tempMat = (resultantMat * transMat +
resultantMat * transMat) % MOD;
resultantMat = tempMat;
resultantMat = tempMat;
resultantMat = tempMat;
resultantMat = tempMat;
}
n = n / 2;
tempMat = (transMat * transMat +
transMat * transMat) % MOD;
tempMat = (transMat * transMat +
transMat * transMat) % MOD;
tempMat = (transMat * transMat +
transMat * transMat) % MOD;
tempMat = (transMat * transMat +
transMat * transMat) % MOD;
transMat = tempMat;
transMat = tempMat;
transMat = tempMat;
transMat = tempMat;
}
return (resultantMat * 1 + resultantMat * 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