Find Nth term of the series 1, 6, 18, 40, 75, ….
Last Updated :
21 Aug, 2022
Given a number n, the task is to find the n-th term in series 1, 6, 18, 40, 75, …
Example:
Input: N = 2
Output: 6
Explanation:
2nd term = (2^2*(2+1))/2
= 6
Input: N = 5
Output: 75
Explanation:
5th term = (5^2*(5+1))/2
= 75
Approach:
Nth term = (N^2*(N+1))/2
Implementation of the above approach is given below:
C++
#include <bits/stdc++.h>
using namespace std;
int nthTerm( int N)
{
int nth = 0;
nth = (N * N * (N + 1)) / 2;
return nth;
}
int main()
{
int N = 5;
cout << nthTerm(N) << endl;
return 0;
}
|
Java
class GFG {
public static int nthTerm( int N)
{
int nth = 0 ;
nth = (N * N * (N + 1 )) / 2 ;
return nth;
}
public static void main(String[] args)
{
int N = 5 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def nthTerm(N):
nth = 0
nth = (N * N * (N + 1 )) / / 2
return nth
N = 5
print (nthTerm(N))
|
C#
using System;
public class GFG {
public static int nthTerm( int N)
{
int nth = 0;
nth = (N * N * (N + 1)) / 2;
return nth;
}
public static void Main( string [] args)
{
int N = 5;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
$nth = 0;
$nth = ( $N * $N * ( $N + 1)) / 2;
return $nth ;
}
$N = 5;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm(N)
{
let nth = 0;
nth = parseInt((N * N * (N + 1)) / 2);
return nth;
}
let N = 5;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1), since no extra space has been taken.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...