Given the series 2, 8, 18, 32, 50…, find the Nth term of the series.
Examples:
Input: N = 1
Output: 2Input: N = 3
Output: 18Input: N = 5
Output: 50
Approach:
For finding the nth term we need to find the relation between n and each term.
1st term = 2 = 2*(12) // 2*first perfect square
2nd term = 8 = 2*(22) // 2*second perfect square
3rd term = 18 = 2*(32) // 2*third perfect square
4th term = 32 = 2*(42) // 2*fourth perfect square
.
.
.
.
.Nth term = 2*(Nth perfect square)
Formula-
TN = 2 * N ^ 2
Illustration-
Input: N = 5
Output: 50
Explanation-
TN = 2 * N ^ 2
= 2* 5 ^ 2
= 2 * 25
= 50
Below is the C++ program to implement the above approach-
C++
// C++ program to implement // the above approach #include <iostream> using namespace std; // Find n-th term of series // 2, 8, 18, 32, 50... int nthTerm( int N) { // Nth perfect square is N * N int Nthperfectsquare = N * N; return 2 * Nthperfectsquare; } // Driver code int main() { int N = 5; cout << nthTerm(N) << endl; return 0; } |
Java
// Java code for the above approach import java.io.*; class GFG { // Find n-th term of series // 2, 8, 18, 32, 50... static int nthTerm( int N) { // Nth perfect square is N * N int Nthperfectsquare = N * N; return 2 * Nthperfectsquare; } // Driver code public static void main (String[] args) { int N = 5 ; System.out.println(nthTerm(N)); } } // This code is contributed by Potta Lokesh |
Python
# Python program to implement # the above approach # Find n-th term of series # 2, 8, 18, 32, 50... def nthTerm(N): # Nth perfect square is N * N Nthperfectsquare = N * N return 2 * Nthperfectsquare # Driver Code if __name__ = = "__main__" : N = 5 print (nthTerm(N)) # This code is contributed by Samim Hossain Mondal. |
C#
using System; public class GFG{ // Find n-th term of series // 2, 8, 18, 32, 50... static int nthTerm( int N) { // Nth perfect square is N * N int Nthperfectsquare = N * N; return 2 * Nthperfectsquare; } // Driver code static public void Main (){ int N = 5; Console.Write(nthTerm(N)); } } // This code is contributed by hrithikgarg03188 |
Javascript
<script> // Javascript program to implement // the above approach // Find n-th term of series // 2, 8, 18, 32, 50... function nthTerm(N) { // Nth perfect square is N * N let Nthperfectsquare = N * N; return 2 * Nthperfectsquare; } // Driver code let N = 5; document.write(nthTerm(N)) // This code is contributed by gfgking. </script> |
50
Time Complexity: O(1), since there is no loop or recursion.
Auxiliary Space: O(1), since no extra space has been taken.