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

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 = 4Output: 48Explanation: As given in the sequence we can see that 4th term is 48
Input: N = 6Output: 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++
Java
Python3
C#
Javascript
// C++ code to find Nth term of series// 0, 4, 18, ...  #include <bits/stdc++.h>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 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
# 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# 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
<script>// Javascript code to find Nth term of series// 0, 4, 18, ...      // Function to find N-th term// of the seriesfunction getNthTerm(N) {    // (pow(N, 3) - pow(N, 2))    return (N * N * N) - (N * N);}  // Driver Code  let N = 4;  // Get the 8th term of the seriesdocument.write(getNthTerm(N));// This code is contributed by gfgking</script>
  
48
 
Time complexity: O(1)Auxiliary space: O(1)
 
gfgking
maths-cube
maths-perfect-square
Sequence and Series
Mathematical
Pattern Searching
Mathematical
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Algorithm to solve Rubik's Cube
Program to convert a given number to words
Program to multiply two matrices
Modular multiplicative inverse
Program to print prime numbers from 1 to N.
KMP Algorithm for Pattern Searching
Rabin-Karp Algorithm for Pattern Searching
Naive algorithm for Pattern Searching
Check if a string is substring of another
Boyer Moore Algorithm for Pattern Searching