Finding nth term of any Polynomial Sequence

Last Updated : 29 Mar, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Solve Problem
Basic
67.13%
13K

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.


Previous Article
Next Article

Similar Reads

Nth term where K+1th term is product of Kth term with difference of max and min digit of Kth term
Given two integers N and D, the task is to find the value of F(N) where the value of F(1) is D, where F(K) is given as: [Tex]F(K+1) = F(K) * (Max_{Digit}(F(K)) - Min_{Digit}(F(K)))[/Tex] Examples: Input: N = 3, D = 487 Output: 15584 Explanation: As F(1) = 487, F(2) = 487 * (maxDigit(487) - minDigit(487)) = 487 * 4 = 1948 F(3) = 1948 * (maxDigit(194
6 min read
Nth term of a sequence formed by sum of current term with product of its largest and smallest digit
Given two numbers N and K, where K represents the starting term of the sequence. The task is to find the Nth term of a sequence formed by sum of current term with product of largest and smallest digit of current term, i.e., AN+1 = AN + max(digits of AN) * min(digits of AN) Examples: Input: K = 1, N = 5 Output: 50 Explanation: A1 = 1 A2 = A1 + minDi
6 min read
Finding the Nth term in a sequence formed by removing digit K from natural numbers
Given the integers N, K and an infinite sequence of natural numbers where all the numbers containing the digit K (1<=K<=9) are removed. The task is to return the Nth number of this sequence. Example: Input: N = 12, K = 2Output: 14Explanation: The sequence generated for the above input would be like this: 1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14
10 min read
Find Nth term of the series where each term differs by 6 and 2 alternately
Given a number N, the task is to find the Nth term of the series where each term differs by 6 and 2 alternately.Examples: Input: N = 6 Output: 24 Explanation: The Nth term is 0 + 6 + 2 + 6 + 2 + 6 + 2 = 24Input: N = 3 Output: 14 Explanation: The Nth term is 0 + 6 + 2 + 6 = 14 Naive Approach: The idea is to iterate from 1 with an increment of 6 and
7 min read
Nth term of given recurrence relation having each term equal to the product of previous K terms
Given two positive integers N and K and an array F[] consisting of K positive integers. The Nth term of the recurrence relation is given by: FN = FN - 1 * FN - 2 * FN - 3 *.......* FN - K The task is to find the Nth term of the given recurrence relation. As the Nth term can be very large, print the Nth term modulo 109 + 7. Examples: Input: N = 5, K
15+ min read
First term from given Nth term of the equation F(N) = (2 * F(N - 1)) % 10^9 + 7
Given an integer N and an integer FN which denotes the Nth term of the linear equation F(N) = (2 * F(N - 1)) % M, where M is 109 + 7, the task is to find the value of F(1). Examples: Input : N = 2, FN = 6 Output:3 Explanation: If F(1) = 3, F(2) = (2 * F(1)) % M = (2 * 3) % M = 6. For F(1) = 3 the given linear equation satisfies the value of F(2). T
8 min read
Find the Nth term of the series where each term f[i] = f[i - 1] - f[i - 2]
Given three integers X, Y and N, the task is to find the Nth term of the series f[i] = f[i - 1] - f[i - 2], i > 1 where f[0] = X and f[1] = Y.Examples: Input: X = 2, Y = 3, N = 3 Output: -2 The series will be 2 3 1 -2 -3 -1 2 and f[3] = -2Input: X = 3, Y = 7, N = 8 Output: 4 Approach: An important observation here is that there will be atmost 6
10 min read
Find nth term of the Dragon Curve Sequence
Dragon Curve Sequence is an infinite binary sequence of 0's and 1's. The first term of the sequence is 1. From the next term, we alternately insert 1 and 0 between each element of the previous term. To understand better refer the following explanations: 1 (starts with 1) "1" 1 "0" 1 and 0 are inserted alternately to the left and right of the previo
9 min read
Count the occurrence of Nth term in first N terms of Van Eck's sequence
Prerequisite: Van Eck's sequence Given a positive integer N, the task is to count the occurrences of Nth term in the first N terms of Van Eck's sequence. Examples: Input: N = 5 Output: 1 Explanation: First 5 terms of Van Eck's Sequence 0, 0, 1, 0, 2 Occurrence of 5th term i.e 2 = 1 Input: 11 Output: 5 Explanation: First 11 terms of Van Eck's Sequen
15 min read
Find first K characters in Nth term of the Thue-Morse sequence
Given two integers N and K, the task is to print the first K bits of the Nth term of the Thue-Morse sequence. The Thue-Morse sequence is a binary sequence. It starts with a "0" as its first term. And then after the next term is generated by replacing "0" with "01" and "1" with "10". Examples: Input: N = 3, K = 2Output: 01Explanation: The 1st term i
6 min read
Program to find Nth term of the Van Eck's Sequence
Given a positive integer N, the task is to print Nth term of the Van Eck's sequence.In mathematics, Van Eck's sequence is an integer sequence which is defined recursively as follows: Let the first term be 0 i.e a0 = 0.Then for n >= 0, if there exists an m < n such thatam = antake the largest such m and set an+1 = n − m;Otherwise an+1 = 0.Star
9 min read
Complete the sequence generated by a polynomial
Given a sequence with some of its term, we need to calculate next K term of this sequence. It is given that sequence is generated by some polynomial, however complex that polynomial can be. Notice polynomial is an expression of the following form: P(x) = a0 + a1 x +a2 x^2 + a3 x^3 …….. + an x^nThe given sequence can always be described by a number
10 min read
Sum of series till N-th term whose i-th term is i^k - (i-1)^k
Given value of N and K. The task is to find the sum of series till N-th term whose i-th term is given by Ti = ik + (i - 1)k. Since the sum of the series can be very large, compute its sum modulo 1000000007. Example: Input : n = 5, k = 2 Output: 25 first 5 term of the series : T1 = ( 1 )2 + ( 1 - 1 )2 = 1 T2 = ( 2 )2 + ( 2 - 1 )2 = 3 T3 = ( 3 )2 + (
9 min read
Count sequences of length K having each term divisible by its preceding term
Given two integer N and K, the task is to find the number of sequence of length K consisting of values from the range [1, N], such that every (i + 1)th element in the sequence is divisible by its preceding ith element. Examples: Input: N = 3, K = 2 Output: 5 Explanation: The 5 sequence are [1, 1], [2, 2], [3, 3], [1, 2], [1, 3] Input: N = 6 K= 4 Ou
10 min read
Finding n-th term of series 3, 13, 42, 108, 235…
Given a number n, find the n-th term in the series 3, 13, 42, 108, 235…Examples: Input : 3 Output : 42 Input : 4 Output : 108 Constraints: 1 <= T <= 100 1 <= N <= 100Naive Approach : The series basically represents sums of natural numbers cube and number of terms multiplied by 2. The first term is the sum of the single number. The secon
6 min read
Program to get the Sum of series: 1 - x^2/2! + x^4/4! -.... upto nth term
This is a mathematical series program where a user must enter the number of terms up to which the sum of the series is to be found. Following this, we also need the value of x, which forms the base of the series. Examples: Input : x = 9, n = 10 Output : -5.1463 Input : x = 5, n = 15 Output : 0.2837 Simple approach : We use two nested loops to compu
10 min read
Program to find Nth term of series 1, 3, 12, 60, 360…
Given a number N. The task is to write a program to find the Nth term in the below series: 1, 3, 12, 60, 360… Examples: Input: 2 Output: 3 Input: 4 Output: 60 Approach: The idea is to first find the factorial of the number (N+1), that is (N+1)!Now, the N-th term in the above series will be: N-th term = (N+1)!/2 Below is the implementation of the ab
5 min read
Program to find the Nth term of series -1, 2, 11, 26, 47......
Given a number N, the task is to find the Nth term of this series: -1, 2, 11, 26, 47, 74, ..... Examples: Input: 3 Output: 11 Explanation: when N = 3 Nth term = ( (3 * N * N) - (6 * N) + 2 ) = ( (3 * 3 * 3) - (6 * 3) + 2 ) = 11 Input: 9 Output: 191 Approach: The Nth term of the given series can be generalised as: Nth term of the series : ( (3 * N *
3 min read
Program to print tetrahedral numbers upto Nth term
Prerequisites: Triangular numbersTetrahedral numbers Given a value n, and the task is to print tetrahedral number series up to nth term.Examples: Input: 5 Output: 1 4 10 20 35 Input: 10 Output: 1 4 10 20 35 56 84 120 165 220 Method 1: Using Triangular Number series: This problem can be easily solved with the fact that Nth Tetrahedral Number is equa
9 min read
Program to print pentatope numbers upto Nth term
Prerequisites: Tetrahedral numbersPentatope Numbers Given a value n, and the task is to print pentatope numbers series up to nth term.Examples: Input: 5 Output: 1 5 15 35 70 Input: 10 Output: 1 5 15 35 70 126 210 330 495 715 Method 1: Using Tetrahedral Number Series: This problem can be easily solved with the fact that Nth Pentatope Number is equal
10 min read
Program to find Nth term of series 9, 23, 45, 75, 113...
Given a number N, the task is to find the Nth term in the given series: 9, 23, 45, 75, 113, 159...... Examples: Input: 4 Output: 113 Explanation: For N = 4 Nth term = ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N = ( 2 * 4 + 3 )*( 2 * 4 + 3 ) - 2 * 4 = 113 Input: 10 Output: 509 Approach: The Nth term of the given series can be generalised as: Nth term of the
4 min read
Program to find Nth term in the series 0, 2, 1, 3, 1, 5, 2, 7, 3,…
Given a number N. The task is to write a program to find the N-th term in the below series: 0, 2, 1, 3, 1, 5, 2, 7, 3, … Examples: Input: N = 5 Output: 1 Input: N = 10 Output: 11 When we look carefully at the series, we find that the series is a mixture of 2 series: Terms at odd positions in the given series forms fibonacci series.Terms at even pos
8 min read
Find Nth term of series 1, 4, 15, 72, 420...
Given a number N. The task is to write a program to find the Nth term in the below series: 1, 4, 15, 72, 420... Examples: Input: 3Output: 15Explanation: For N = 3, we know that the factorial of 3 is 6 Nth term = 6*(3+2)/2 Input: 6Output: 2880Explanation: For N = 6, we know that the factorial of 6 is 720 Nth term = 620*(6+2)/2 = 2880 The idea is to
7 min read
Find Nth term of the series 0, 2, 4, 8, 12, 18...
Given a number N. The task is to write a program to find the Nth term in the below series: 0, 2, 4, 8, 12, 18... Examples: Input: 3 Output: 4 For N = 3 Nth term = ( 3 + ( 3 - 1 ) * 3 ) / 2 = 4 Input: 5 Output: 12 On observing carefully, the Nth term in the above series can be generalized as: Nth term = ( N + ( N - 1 ) * N ) / 2 Below is the impleme
3 min read
Program to find the Nth term of the series 3, 20, 63, 144, 230, ……
Given a series and a number N. The task is to find the Nth term of the given series: 3, 20, 63, 144, 230 ..... Examples: Input: N = 4 Output: 144 When n = 4 nth term = 2 ( n * n * n ) + n * n = 2 ( 4 * 4 * 4 ) + 4 * 4 = 144 Input: N = 10 Output: 2100 Approach: We can find the general term (Tn) of the given series. [Tex]series\: can\: be\: written\:
3 min read
Program to find the Nth term of series 5, 10, 17, 26, 37, 50, 65, 82, ...
Given a number N. The task is to write a program to find the Nth term of the below series: 5, 10, 17, 26, 37, 50, 65, 82, ...(N Terms) Examples: Input: N = 4 Output: 82 For N = 4 4th Term = ( 4 * 4 + 2 * 4 + 2) = 26 Input: N = 10 Output: 122 Approach: The generalized Nth term of this series: [Tex]nth\: Term\: of\: the\: series\: = n^2+2n+2[/Tex] Be
3 min read
Program to find the Nth term of series 0, 4, 14, 30, 51, 80, 114, 154, 200, ...
Given a number N. The task is to write a program to find the Nth term of the below series: 0, 4, 14, 30, 51, 80, 114, 154, 200, …(N Terms) Examples: Input: N = 4 Output: 82 For N = 4 4th Term = ( 4 * 4 - 2 * 4 + 2) = 10 Input: N = 10 Output: 122 Approach: The generalized Nth term of this series: [Tex]nth\: Term\: of\: the\: series\: = n^2-2n+2[/Tex
3 min read
Find Nth term of the series 3, 14, 39, 84...
Given a number N. The task is to write a program to find the Nth term in the below series: 3, 14, 39, 84... Examples: Input: 3 Output: 39 For N = 3 Nth term = ( 3*3*3 ) + ( 3*3 ) + 3 = 39 Input: 4 Output: 84 Formula to calculate the Nth term of the series: Nth term = ( N*N*N ) + ( N*N ) + N Below is the required implementation: C/C++ Code // CPP pr
3 min read
Program to find the Nth term of the series 0, 14, 40, 78, 124, ...
Given a number N. The task is to write a program to find the Nth term of the below series: 0, 14, 40, 78, 124 ...(N Terms) Examples: Input: N = 4 Output: 78 For N = 4 Sum(upto 4 terms) = ( 6 * n * n - 4 * n - 2) = ( 6 * 4 * 4 - 4 * 4 - 2) = 78 Input: N = 10 Output: 557 Approach: The generalized Nth term of this series:[Tex]nth\: Term\: of\: the\: s
3 min read
Program to find the Nth term of the series 0, 5, 14, 27, 44, ........
Given a number N. The task is to write a program to find the Nth term in the below series: 0, 5, 14, 27, 44 ...(Nth Term) Examples: Input: N = 4 Output: 27 For N = 4, Nth term = ( 2 * N * N - N - 1 ) = ( 2 * 4 * 4 - 4 - 1 ) = 27 Input: N = 10 Output: 188 Approach: The generalized Nth term of this series: Nth Term: 2 * N * N - N - 1 Below is the req
3 min read