# Find Nth term (A matrix exponentiation example)

We are given a recursive function that describes Nth terms in form of other terms. In this article we have taken specific example.

Now you are given n, and you have to find out nth term using above formula.

Examples:

Input : n = 2 Output : 5 Input : n = 3 Output :13

Prerequisite :

**Basic Approach:**This problem can be solved by simply just iterating over the n terms. Every time you find a term, using this term find next one and so on. But time complexity of this problem is of order O(n).

**Optimized Approach**

All such problem where a term is a function of other terms in linear fashion. Then these can be solved using Matrix (Please refer : Matrix Exponentiation ). First we make transformation matrix and then just use matrix exponentiation to find Nth term.

**Step by Step method includes:**

**Step 1. Determine k the number of terms on which T(i) depends.**

In our example T(i) depends on two terms.so, k = 2

**Step 2. Determine initial values**

As in this article T0=1, T1=1 are given.

**Step 3. Determine TM, the transformation matrix.**

This is the most important step in solving recurrence relation. In this step, we have to make matrix of dimension k*k.

Such that

T(i)=TM*(initial value vector)

Here **initial value vector ** is vector that contains intial value.we name this vector as **initial**.

Below is C++ program to implement above approach

`// CPP program to find n-th term of a recursive ` `// function using matrix exponentiation. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` `#define MOD 1000000009 ` ` ` `#define ll long long int ` ` ` ` ` `ll power(ll n) ` `{ ` ` ` `if` `(n <= 1) ` ` ` `return` `1; ` ` ` ` ` `// This power function returns first row of ` ` ` `// {Transformation Matrix}^n-1*Initial Vector ` ` ` `n--; ` ` ` ` ` `// This is an identity matrix. ` ` ` `ll res[2][2] = { 1, 0, 0, 1 }; ` ` ` ` ` `// this is Transformation matrix. ` ` ` `ll tMat[2][2] = { 2, 3, 1, 0 }; ` ` ` ` ` `// Matrix exponentiation to calculate power of {tMat}^n-1 ` ` ` `// store res in "res" matrix. ` ` ` `while` `(n) { ` ` ` ` ` `if` `(n & 1) { ` ` ` `ll tmp[2][2]; ` ` ` `tmp[0][0] = (res[0][0] * tMat[0][0] + ` ` ` `res[0][1] * tMat[1][0]) % MOD; ` ` ` `tmp[0][1] = (res[0][0] * tMat[0][1] + ` ` ` `res[0][1] * tMat[1][1]) % MOD; ` ` ` `tmp[1][0] = (res[1][0] * tMat[0][0] + ` ` ` `res[1][1] * tMat[1][0]) % MOD; ` ` ` `tmp[1][1] = (res[1][0] * tMat[0][1] + ` ` ` `res[1][1] * tMat[1][1]) % MOD; ` ` ` `res[0][0] = tmp[0][0]; ` ` ` `res[0][1] = tmp[0][1]; ` ` ` `res[1][0] = tmp[1][0]; ` ` ` `res[1][1] = tmp[1][1]; ` ` ` `} ` ` ` `n = n / 2; ` ` ` `ll tmp[2][2]; ` ` ` `tmp[0][0] = (tMat[0][0] * tMat[0][0] + ` ` ` `tMat[0][1] * tMat[1][0]) % MOD; ` ` ` `tmp[0][1] = (tMat[0][0] * tMat[0][1] + ` ` ` `tMat[0][1] * tMat[1][1]) % MOD; ` ` ` `tmp[1][0] = (tMat[1][0] * tMat[0][0] + ` ` ` `tMat[1][1] * tMat[1][0]) % MOD; ` ` ` `tmp[1][1] = (tMat[1][0] * tMat[0][1] + ` ` ` `tMat[1][1] * tMat[1][1]) % MOD; ` ` ` `tMat[0][0] = tmp[0][0]; ` ` ` `tMat[0][1] = tmp[0][1]; ` ` ` `tMat[1][0] = tmp[1][0]; ` ` ` `tMat[1][1] = tmp[1][1]; ` ` ` `} ` ` ` ` ` `// res store {Transformation matrix}^n-1 ` ` ` `// hence will be first row of res*Initial Vector. ` ` ` `return` `(res[0][0] * 1 + res[0][1] * 1) % MOD; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `ll n = 3; ` ` ` `cout << power(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

13

Time Complexity : O(Log n)

The same idea is used to find n-th Fibonacci number in O(Log n)

## Recommended Posts:

- Matrix Exponentiation
- Modular exponentiation (Recursive)
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Modular Exponentiation (Power in Modular Arithmetic)
- Find sub-matrix with the given sum
- Find the mean vector of a Matrix
- Find row with maximum sum in a Matrix
- Find if given matrix is Toeplitz or not
- Find all permuted rows of a given row in a matrix
- Program to find the Sum of each Row and each Column of a Matrix
- Find orientation of a pattern in a matrix
- Find a specific pair in Matrix
- Find whether there is path between two cells in matrix
- Find number of cavities in a matrix
- Find single Movement in a Matrix

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.