Find the nth term of the series 0, 8, 64, 216, 512, . . .
Last Updated :
24 Jun, 2022
Given an integer N, the task is to find the Nth term of the following series:
0, 8, 64, 216, 512, 1000, 1728, . . .
Examples:
Input: N = 6
Output: 1000
Input: N = 5
Output: 512
Approach:
- Given series 0, 8, 64, 216, 512, 1000, 1728, … can also be written as 0 * (02), 2 * (22), 4 * (42), 6 * (62), 8 * (82), 10 * (102), …
- Observe that 0, 2, 4, 6, 10, … is in AP and the nth term of this series can be found using the formula term = a1 + (n – 1) * d where a1 is the first term, n is the term position and d is the common difference.
- To get the term in the original series, term = term * (term2) i.e. term3.
- Finally print the term.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
long term( int n)
{
int d = 2;
int a1 = 0;
int An = a1 + (n - 1) * d;
An = pow (An, 3);
return An;
}
int main()
{
int n = 5;
cout << term(n);
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
public class GFG {
static int nthTerm( int n)
{
int d = 2 , a1 = 0 ;
int An = a1 + (n - 1 ) * d;
return ( int )Math.pow(An, 3 );
}
public static void main(String[] args)
{
int n = 5 ;
System.out.println(nthTerm(n));
}
}
|
Python3
def term(n):
d = 2
a1 = 0
An = a1 + (n - 1 ) * d
An = An * * 3
return An;
n = 5
print (term(n))
|
C#
using System;
public class GFG {
static int nthTerm( int n)
{
int d = 2, a1 = 0;
int An = a1 + (n - 1) * d;
return ( int )Math.Pow(An, 3);
}
public static void Main()
{
int n = 5;
Console. WriteLine(nthTerm(n));
}
}
|
PHP
<?php
function term( $n )
{
$d = 2;
$a1 = 0;
$An = $a1 +( $n -1)* $d ;
return pow( $An , 3);
}
$n = 5;
echo term( $n );
?>
|
Javascript
<script>
function term(n)
{
let d = 2;
let a1 = 0;
An=a1+(n-1)*d;
return Math.pow(An, 3);
}
let n = 5;
document.write( term(n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...