n-th term in series 2, 12, 36, 80, 150….
Given a series 2, 12, 36, 80, 150.. Find the n-th term of the series.
Examples :
Input : 2
Output : 12
Input : 4
Output : 80
If we take a closer look, we can notice that series is sum of squares and cubes of natural numbers (1, 4, 9, 16, 25, …..) + (1, 8, 27, 64, 125, ….).
Therefore n-th number of the series is n^2 + n^3
C++
#include <iostream>
using namespace std;
int nthTerm( int n)
{
return (n * n) + (n * n * n);
}
int main()
{
int n = 4;
cout << nthTerm(n);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int nthTerm( int n)
{
return (n * n) + (n * n * n);
}
public static void main(String[] args)
{
int n = 4 ;
System.out.print(nthTerm(n));
}
}
|
Python3
def nthTerm( n ):
return (n * n) + (n * n * n)
n = 4
print ( nthTerm(n))
|
C#
using System;
class GFG
{
public static int nthTerm( int n)
{
return (n * n) + (n * n * n);
}
public static void Main()
{
int n = 4;
Console.WriteLine(nthTerm(n));
}
}
|
PHP
<?php
function nthTerm( $n )
{
return ( $n * $n ) + ( $n * $n * $n );
}
$n = 4;
echo (nthTerm( $n ));
?>
|
Javascript
<script>
function nthTerm(n)
{
return (n * n) + (n * n * n);
}
let n = 4;
document.write(nthTerm(n));
</script>
|
Output :
80
Time complexity: O(1) as only single step is required to calculate nth term from given formula
Auxiliary Space: O(1)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...