Find nth term of the series 5 2 13 41
Last Updated :
22 Sep, 2022
Given a number N, the task is to find the nth term of the series
5, 2, 19, 13, 41, 31, 71, 57….
It is given that value of n can range between 1 and 10000.
Examples:
Input: N = 4
Output:13
Input: N = 15
Output:272
Approach: The problem looks very hard but approach is very simple. If the value of n is given as an odd number, the nth term will be ( ( n + 1 ) ^ 2 ) + n.
Otherwise, it will be ( ( n – 1 ) ^ 2 ) + n.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
int nthTermOfTheSeries( int n)
{
int nthTerm;
if (n % 2 == 0)
nthTerm = pow (n - 1, 2) + n;
else
nthTerm = pow (n + 1, 2) + n;
return nthTerm;
}
int main()
{
int n;
n = 8;
cout << nthTermOfTheSeries(n) << endl;
n = 12;
cout << nthTermOfTheSeries(n) << endl;
n = 102;
cout << nthTermOfTheSeries(n) << endl;
n = 999;
cout << nthTermOfTheSeries(n) << endl;
n = 9999;
cout << nthTermOfTheSeries(n) << endl;
return 0;
}
|
C
#include <math.h>
#include <stdio.h>
int nthTermOfTheSeries( int n)
{
int nthTerm;
if (n % 2 == 0)
nthTerm = pow (n - 1, 2) + n;
else
nthTerm = pow (n + 1, 2) + n;
return nthTerm;
}
int main()
{
int n;
n = 8;
printf ( "%d\n" , nthTermOfTheSeries(n));
n = 12;
printf ( "%d\n" , nthTermOfTheSeries(n));
n = 102;
printf ( "%d\n" , nthTermOfTheSeries(n));
n = 999;
printf ( "%d\n" , nthTermOfTheSeries(n));
n = 9999;
printf ( "%d\n" , nthTermOfTheSeries(n));
return 0;
}
|
Java
import java.lang.Math;
class GFG
{
static long nthTermOfTheSeries( int n)
{
long nthTerm;
if (n % 2 == 0 )
nthTerm = ( long )Math.pow(n - 1 , 2 ) + n;
else
nthTerm = ( long )Math.pow(n + 1 , 2 ) + n;
return nthTerm;
}
public static void main(String[] args)
{
int n;
n = 8 ;
System.out.println( nthTermOfTheSeries(n));
n = 12 ;
System.out.println( nthTermOfTheSeries(n));
n = 102 ;
System.out.println( nthTermOfTheSeries(n));
n = 999 ;
System.out.println( nthTermOfTheSeries(n));
n = 9999 ;
System.out.println( nthTermOfTheSeries(n));
}
}
|
Python3
from math import pow
def nthTermOfTheSeries(n):
if (n % 2 = = 0 ):
nthTerm = pow (n - 1 , 2 ) + n
else :
nthTerm = pow (n + 1 , 2 ) + n
return nthTerm
if __name__ = = '__main__' :
n = 8
print ( int (nthTermOfTheSeries(n)))
n = 12
print ( int (nthTermOfTheSeries(n)))
n = 102
print ( int (nthTermOfTheSeries(n)))
n = 999
print ( int (nthTermOfTheSeries(n)))
n = 9999
print ( int (nthTermOfTheSeries(n)))
|
C#
using System;
class GFG
{
static long nthTermOfTheSeries( int n)
{
long nthTerm;
if (n % 2 == 0)
nthTerm = ( long )Math.Pow(n - 1, 2) + n;
else
nthTerm = ( long )Math.Pow(n + 1, 2) + n;
return nthTerm;
}
public static void Main()
{
int n;
n = 8;
Console.WriteLine(nthTermOfTheSeries(n));
n = 12;
Console.WriteLine( nthTermOfTheSeries(n));
n = 102;
Console.WriteLine( nthTermOfTheSeries(n));
n = 999;
Console.WriteLine( nthTermOfTheSeries(n));
n = 9999;
Console.WriteLine( nthTermOfTheSeries(n));
}
}
|
PHP
<?php
function nthTermOfTheSeries( $n )
{
if ( $n % 2 == 0)
$nthTerm = pow( $n - 1, 2) + $n ;
else
$nthTerm = pow( $n + 1, 2) + $n ;
return $nthTerm ;
}
$n = 8;
echo nthTermOfTheSeries( $n ) . "\n" ;
$n = 12;
echo nthTermOfTheSeries( $n ) . "\n" ;
$n = 102;
echo nthTermOfTheSeries( $n ) . "\n" ;
$n = 999;
echo nthTermOfTheSeries( $n ) . "\n" ;
$n = 9999;
echo nthTermOfTheSeries( $n ) . "\n" ;
?>
|
Javascript
<script>
function nthTermOfTheSeries(n)
{
let nthTerm;
if (n % 2 == 0)
nthTerm = Math.pow(n - 1, 2) + n;
else
nthTerm = Math.pow(n + 1, 2) + n;
return nthTerm;
}
let n;
n = 8;
document.write(nthTermOfTheSeries(n) + "<br>" );
n = 12;
document.write(nthTermOfTheSeries(n) + "<br>" );
n = 102;
document.write(nthTermOfTheSeries(n) + "<br>" );
n = 999;
document.write(nthTermOfTheSeries(n) + "<br>" );
n = 9999;
document.write(nthTermOfTheSeries(n) + "<br>" );
</script>
|
Output:
57
133
10303
1000999
100009999
Time Complexity: O(1) as it is doing constant operations
Auxiliary Space: O(1) since no extra array is used space taken by this algorithm is constant
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...