Given a few terms of a sequence, we are often asked to find the expression for the nth term of this sequence. While there is a multitude of ways to do this, In this article, we discuss an algorithmic approach which will give the correct answer for any polynomial expression. Note that this method fails for trigonometric, exponential or other transcendental sequences.

We will take a few examples and discuss the method as we solve these examples.

**Example 1**

Let us take the sequence

S = 3, 9, 15, 21, ....

Trivial Solution

We can easily see that this is an A.P series. The starting term is 3 and the common difference is 6. Hence the nth term of the sequence is 3 + (n-1) * 6.

Our Solution

Since we know that an nth order polynomial has the form:

Polynomial = Pn = a0 + a1*x^1 + a2*x^2 + ... + an*x^n

Since we already know a few terms of the sequence, we can substitute these values in the above expression and obtain some equations. Then we need to solve this system of linear equations and obtain the coefficients a0, a1, … an.

The trick is to somehow know the order of the polynomial.

This is simple enough to do if we remember that the nth order forward difference is constant for an nth order polynomial. Also all higher order forward differences are 0. Let us obtain the forward differences for the series

3 9 15 21 27 ... 6 6 6 6

Since the first forward difference is constant, we conclude that the sequence is a first order polynomial.

1st order polynomial Sn = a0 + a1*n For n = 1, Sn = 3 3 = a0 + a1 .... (1) For n = 2, Sn = 9 9 = a0 + 2*a1 .... (2) Solving (1) and (2) we get a1 = 6 and a0 = -3 Hence we obtain Sn = -3 + 6*n

Note that we obtain slightly different formulations of the sequence in the above two methods. But both these formulations are correct and can easily be converted from one form to another

**Example 2**

Lets us take the sequence S = 9 24 47 78 117 164 219 ... 15 23 31 39 47 55 8 8 8 8 8 Since the 2n order forward difference is constant, the Sequence is a 2nd order polynomial S = a0 + a1*n + a2 *n*n Substituting n = 1, 2, 3 and corresponding entries we get 9 = a0 + a1 + a2 ... (1) 24 = a0 + 2*a1 + 4*a2 ... (2) 47 = a0 + 3*a1 + 9*a3 ... (3) Writing matrices for above equations A = 1 1 1 1 2 4 1 3 9 B = 9 24 47 X = [a0 a1 a2] = inv(A)*B = [ 2 3 4] Hence a0 = 2, a1 = 3, a2 = 4 Therefore S = 2 + 3*n + 4 * n * n

**Example 3**

S = 28, 168, 516, 1168, 2220 ... Forward Differences 28 168 516 1168 2220 140 348 652 1052 208 304 400 96 96 The Sequence is a third order polynomial S = a0 + a1*n + a2*n*n + a3*n*n*n Substituting n = 1, 2, 3, 4 and corresponding values 28 = a0 + a1 + a2 + a3 168 = a0 + 2*a1 + 4*a2*a2 + 8*a3*a3*a3 516 = a0 + 3*a1 + 9*a2*a2 + 27*a3*a3*a3 1168 = a0 + 4*a1 + 16*a2*a2 + 64*a3*a3*a3 Writing matrices for above equations A = 1 1 1 1 1 2 4 8 1 3 9 27 1 4 16 64 B = 28, 168, 516, 1168 X = [a0 a1 a2 a3] = inv(A)*B = [0, 4, 8, 16] Therefore S = 4*n + 8*n*n + 16*n*n*n

**Note:** To solve an nth order Sequence we need to solve a system of n equations. These equations can either be solved by hand or by using matrix-based math calculators like MATLAB, Octave etc. A free online version of Octave is available at Octave Online.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Find Nth term of the series where each term differs by 6 and 2 alternately
- First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
- Nth term of given recurrence relation having each term equal to the product of previous K terms
- Python | Finding Solutions of a Polynomial Equation
- Find nth term of the Dragon Curve Sequence
- Program to find Nth term of the Van Eck's Sequence
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Complete the sequence generated by a polynomial
- Finding n-th term of series 3, 13, 42, 108, 235…
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Count sequences of length K having each term divisible by its preceding term
- Program to print the sum of the given nth term
- Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
- Program to find Nth term in the given Series
- Program to find the Nth term of the series 3, 7, 13, 21, 31.....
- Program to find Nth term of series 1, 3, 12, 60, 360…
- Program to find the Nth term of series -1, 2, 11, 26, 47......

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.