Program to find Nth term of the series 3 , 5 , 21 , 51 , 95 , …
Last Updated :
07 Aug, 2022
Given a number N, the task is to find the Nth term of this series:
3, 5, 21, 51, 95, ….
Examples:
Input: N = 4
Output: 51
Explanation:
Nth term = (7 * pow(N, 2) - 19 * N + 15)
= (7 * pow(4, 2) - 19 * 4 + 15)
= 51
Input: N = 10
Output: 525
Approach:
The Nth term of the given series can be generalized as:
[Tex] [/Tex]
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
int getNthTerm( long long int N)
{
return (7 * pow (N, 2) - 19 * N + 15);
}
int main()
{
long long int N = 4;
cout << getNthTerm(N);
return 0;
}
|
Java
import java.util.*;
class solution
{
static long getNthTerm( long N)
{
return ( 7 *( int ) Math.pow(N, 2 ) - 19 * N + 15 );
}
public static void main(String arr[])
{
long N = 4 ;
System.out.println(getNthTerm(N));
}
}
|
Python3
def getNthTerm(N):
return ( 7 * pow (N, 2 ) - 19 * N + 15 )
if __name__ = = '__main__' :
N = 4
print (getNthTerm(N))
|
C#
using System;
class GFG
{
static long getNthTerm( long N)
{
return (7 *( int ) Math.Pow(N, 2) -
19 * N + 15);
}
static public void Main ()
{
long N = 4;
Console.Write(getNthTerm(N));
}
}
|
PHP
<?php
function getNthTerm( $N )
{
return (7 * pow( $N , 2) -
19 * $N + 15);
}
$N = 4;
echo getNthTerm( $N );
?>
|
Javascript
<script>
function getNthTerm(N)
{
return (7 * Math.pow(N, 2) - 19 * N + 15);
}
let N = 4;
document.write( getNthTerm(N) );
</script>
|
Time Complexity: O(1)
Auxiliary Space : O(1) since using constant variables
Share your thoughts in the comments
Please Login to comment...