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