Find Nth term of the series 1, 5, 32, 288 …
Given a number n, the task is to find the n-th term in series 1, 5, 32, 288 …
Examples:
Input: N = 3
Output: 32
Explanation:
3rd term = 3^3 + 2^2 + 1^1
= 32
Input: N = 4
Output: 288
Explanation:
4th term = 4^4 + 3^3 + 2^2 + 1^1
= 288Approach:
Nth term = n^n + (n-1)^(n-1) + (n-2)^(n-2) + ……..1^1.
Implementation of the above approach is given below:
C++
// CPP code to generate 'Nth' terms// of this sequence#include <bits/stdc++.h>using namespace std;// Function to generate a fixed \numberint nthTerm(int N){ int nth = 0, i; // Finding nth term for (i = N; i > 0; i--) { nth += pow(i, i); } return nth;}// Driver Methodint main(){ int N = 3; cout << nthTerm(N) << endl; return 0;} |
Java
// Java code to generate 'Nth' terms// of this sequenceimport java.lang.Math;class GFG { // Function to generate a fixed \number public static int nthTerm(int N) { int nth = 0, i; // Finding nth term for (i = N; i > 0; i--) { nth += Math.pow(i, i); } return nth; } // Driver Method public static void main(String[] args) { int N = 3; System.out.println(nthTerm(N)); }}// This code is contributed by 29AjayKumar |
Python3
# Python3 code to generate 'Nth'# terms of this sequence# Function to generate a# fixed numberdef nthTerm(N): nth = 0 # Finding nth term for i in range(N, 0, -1): nth += pow(i, i) return nth# Driver codeN = 3print(nthTerm(N))# This code is contributed# by Shrikant13 |
C#
// C# code to generate 'Nth' terms// of this sequenceusing System;class GFG{ // Function to generate a fixed \number public static int nthTerm(int N) { int nth = 0, i; // Finding nth term for (i = N; i > 0; i--) { nth +=(int)Math.Pow(i, i); } return nth; } // Driver Method public static void Main() { int N = 3; Console.WriteLine(nthTerm(N)); }}// This code is contributed by Code_Mech. |
PHP
<?php// PHP code to generate 'Nth' terms// of this sequence// Function to generate a fixed \numberfunction nthTerm($N){ $nth = 0; $i; // Finding nth term for ($i = $N; $i > 0; $i--) { $nth += pow($i, $i); } return $nth;}// Driver Code$N = 3;echo(nthTerm($N));// This code is contributed by Code_Mech.?> |
Javascript
<script>// Javascript code to generate 'Nth' terms// of this sequence// Function to generate a fixed \numberfunction nthTerm(N){ let nth = 0, i; // Finding nth term for (i = N; i > 0; i--) { nth += Math.pow(i, i); } return nth;}// Driver Methodlet N = 3;document.write(nthTerm(N));// This code is contributed by subham348.</script> |
Output:
32
Time Complexity: O(N)
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.


