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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
17 Feb, 2023
Like Article
Save Article