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 DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Python | Finding Solutions of a Polynomial Equation
- Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
- Complete the sequence generated by a polynomial
- Finding n-th term of series 3, 13, 42, 108, 235…
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Check if the n-th term is odd or even in a Fibonacci like sequence
- Program to find Nth term of the Van Eck's Sequence
- Find nth term of the Dragon Curve Sequence
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Print the sequence of size N in which every term is sum of previous K terms
- Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
- Count sequences of length K having each term divisible by its preceding term
- Find Nth term of the series where each term differs by 6 and 2 alternately
- Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
- Sum of series till N-th term whose i-th term is i^k - (i-1)^k
- Sgn value of a polynomial
- Integration in a Polynomial for a given value
- Content of a Polynomial
- Program for Derivative of a Polynomial
- Program to differentiate the given Polynomial

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.