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.
Input : n = 2 Output : 5 Input : n = 3 Output :13
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).
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.
T(i)=TM*(initial value vector)
Here initial value vector is vector that contains intial value.we name this vector as initial.
Below is the program to implement above approach
Time Complexity : O(Log n)
The same idea is used to find n-th Fibonacci number in O(Log n)
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Matrix Exponentiation
- Expected number of moves to reach the end of a board | Matrix Exponentiation
- Modular Exponentiation (Power in Modular Arithmetic)
- Modular exponentiation (Recursive)
- Modular Exponentiation of Complex Numbers
- Find the Nth term of the series 2, 15, 41, 80, 132...
- Count sequences of length K having each term divisible by its preceding term
- Nth term of a recurrence relation generated by two given arrays
- Find trace of matrix formed by adding Row-major and Column-major order of same matrix
- Find the Nth Mosaic number
- Recursive Approach to find nth node from the end in the linked list
- Find the Nth natural number which is not divisible by A
- Fast Doubling method to find the Nth Fibonacci number
- Find Nth number in a sequence which is not a multiple of a given number
- Find the Nth row in Pascal's Triangle
- N-th term of George Cantor set of rational numbers
- Print the sequence of size N in which every term is sum of previous K terms
- K-th term from given N merged Arithmetic Progressions
- Maximize sum of N X N upper left sub-matrix from given 2N X 2N 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 email@example.com. 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.