Find N-th term in the series 0, 4, 18, 48, 100 …

• Last Updated : 06 Dec, 2021

Given a series 0, 4, 18, 48, 100 . . .  and an integer N, the task is to find the N-th term of the series.

Examples:

Input: N = 4
Output: 48
Explanation: As given in the sequence we can see that 4th term is 48

Input: N = 6
Output: 180

Approach: Consider the below observation:

For N = 2, the 2nd term is 4, which can be represented as 8 – 4, i.e. 23 – 22

For N = 3, the 3rd term is 18, which can be represented as 27 – 9, i.e. 33 – 32

For N = 4, the 4th term is 18, which can be represented as 27 – 9, i.e. 43 – 42

.

.

.

Similarly, The N-th term of this series can be represented as N3 – N2

So for any N, find the square of that N and subtract it from the cube of the number.

Below is the implementation of the above approach:

C++

 // C++ code to find Nth term of series// 0, 4, 18, ...  #include using namespace std;  // Function to find N-th term// of the seriesint getNthTerm(int N){    // (pow(N, 3) - pow(N, 2))    return (N * N * N) - (N * N);}  // Driver Codeint main(){    int N = 4;      // Get the 8th term of the series    cout << getNthTerm(N);    return 0;}

Java

 // Java code to find Nth term of series// 0, 4, 18, ...class GFG{        // Function to find N-th term    // of the series    public static int getNthTerm(int N)     {                // (pow(N, 3) - pow(N, 2))        return (N * N * N) - (N * N);    }      // Driver Code    public static void main(String args[])    {        int N = 4;          // Get the 8th term of the series        System.out.println(getNthTerm(N));    }}  // This code is contributed by gfgking

Python3

 # Python code to find Nth term of series# 0, 4, 18, ...  # Function to find N-th term# of the seriesdef getNthTerm(N):        # (pow(N, 3) - pow(N, 2))    return (N * N * N) - (N * N);  # Driver CodeN = 4;  # Get the 8th term of the seriesprint(getNthTerm(N));  # This code is contributed by gfgking

C#

 // C# code to find Nth term of series// 0, 4, 18, ...  using System;class GFG {    // Function to find N-th term    // of the series    public static int getNthTerm(int N) {        // (pow(N, 3) - pow(N, 2))        return (N * N * N) - (N * N);    }      // Driver Code    public static void Main() {        int N = 4;          // Get the 8th term of the series        Console.Write(getNthTerm(N));    }}  // This code is contributed by gfgking

Javascript



Output
48

Time complexity: O(1)
Auxiliary space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up