Program to find Nth term of series 9, 23, 45, 75, 113…
Given a number N, the task is to find the Nth term in the given series:
9, 23, 45, 75, 113, 159......
Examples:
Input: 4
Output: 113
Explanation:
For N = 4
Nth term = ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N
= ( 2 * 4 + 3 )*( 2 * 4 + 3 ) - 2 * 4
= 113
Input: 10
Output: 509
Approach:
The Nth term of the given series can be generalised as:
Nth term of the series : ( 2 * N + 3 )*( 2 * N + 3 ) - 2 * N
Below is the implementation of the above problem:
Program:
C++
#include <iostream>
using namespace std;
int nthTerm( int N)
{
return (2 * N + 3) * (2 * N + 3) - 2 * N;
}
int main()
{
int N = 4;
cout << nthTerm(N);
return 0;
}
|
Java
class GFG
{
static int nthTerm( int N)
{
return ( 2 * N + 3 ) *
( 2 * N + 3 ) - 2 * N;
}
public static void main(String[] args)
{
int N = 4 ;
System.out.println(nthTerm(N));
}
}
|
Python3
def nthTerm(N):
return (( 2 * N + 3 ) *
( 2 * N + 3 ) - 2 * N);
n = 4
print (nthTerm(n))
|
C#
using System;
class GFG
{
static int nthTerm( int N)
{
return (2 * N + 3) *
(2 * N + 3) - 2 * N;
}
public static void Main()
{
int N = 4;
Console.WriteLine(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
return (2 * $N + 3) *
(2 * $N + 3) - 2 * $N ;
}
$N = 4;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm( N)
{
return (2 * N + 3) * (2 * N + 3) - 2 * N;
}
let N = 4;
document.write( nthTerm(N));
</script>
|
Time Complexity: O(1)
Space Complexity: O(1) since using constant variables
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
19 Jul, 2022
Like Article
Save Article