Program to find the Nth term of series -1, 2, 11, 26, 47……
Last Updated :
19 Jul, 2022
Given a number N, the task is to find the Nth term of this series:
-1, 2, 11, 26, 47, 74, .....
Examples:
Input: 3
Output: 11
Explanation:
when N = 3
Nth term = ( (3 * N * N) - (6 * N) + 2 )
= ( (3 * 3 * 3) - (6 * 3) + 2 )
= 11
Input: 9
Output: 191
Approach:
The Nth term of the given series can be generalised as:
Nth term of the series : ( (3 * N * N) - (6 * N) + 2 )
Below is the implementation of the above problem:
Program:
C++
#include <iostream>;
using namespace std;
int nthTerm( int N)
{
return ((3 * N * N) - (6 * N) + 2);
}
int main()
{
int N = 3;
cout << nthTerm(N);
return 0;
}
|
Java
class GFG {
static int nthTerm( int N)
{
return (( 3 * N * N) - ( 6 * N) + 2 );
}
public static void main(String[] args) {
int N = 3 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def nthTerm(N):
return (( 3 * N * N) - ( 6 * N) + 2 );
if __name__ = = '__main__' :
n = 3
print (nthTerm(n))
|
C#
using System;
class GFG
{
static int nthTerm( int N)
{
return ((3 * N * N) - (6 * N) + 2);
}
public static void Main()
{
int N = 3;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
return ((3 * $N * $N ) -
(6 * $N ) + 2);
}
$N = 3;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm(N)
{
return ((3 * N * N) - (6 * N) + 2);
}
let N = 3;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(1)
Space Complexity: O(1) since using constant variables
Share your thoughts in the comments
Please Login to comment...