Find N-th term in the series 0, 4, 18, 48, 100 …
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 48Input: 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 <bits/stdc++.h> using namespace std; // Function to find N-th term // of the series int getNthTerm( int N) { // (pow(N, 3) - pow(N, 2)) return (N * N * N) - (N * N); } // Driver Code int 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 series def getNthTerm(N): # (pow(N, 3) - pow(N, 2)) return (N * N * N) - (N * N); # Driver Code N = 4 ; # Get the 8th term of the series print (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
<script> // Javascript code to find Nth term of series // 0, 4, 18, ... // Function to find N-th term // of the series function 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 series document.write(getNthTerm(N)); // This code is contributed by gfgking </script> |
48
Time complexity: O(1)
Auxiliary space: O(1)
Please Login to comment...