Find Nth term of the series 5, 13, 25, 41, 61…
Given a number N. The task is to write a program to find the Nth term in the below series:
5, 13, 25, 41, 61...
Examples:
Input : 3
Output : 25
For N = 3
Nth term = 3*3 + (3+1)*(3+1)
= 25
Input : 5
Output : 61
On observing carefully, the Nth term of the given series can be generalised as:
Nth term = N2 + (N+1)2
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
int nthTerm( int N)
{
return N * N + (N + 1) * (N + 1);
}
int main()
{
int N = 3;
cout << nthTerm(N);
return 0;
}
|
Java
import java.io.*;
class Nth {
public static int nthTerm( int N)
{
return N * N + (N + 1 ) * (N + 1 );
}
public static void main(String[] args)
{
int N = 3 ;
System.out.println(nthTerm(N));
}
}
|
Python 3
def nthTerm(N) :
return N * N + (N + 1 ) * (N + 1 )
if __name__ = = "__main__" :
N = 3
print (nthTerm(N))
|
C#
using System;
class GFG
{
public static int nthTerm( int N)
{
return N * N + (N + 1) * (N + 1);
}
public static void Main()
{
int N = 3;
Console.Write(nthTerm(N));
}
}
|
PHP
<?php
function nthTerm( $N )
{
return $N * $N + ( $N + 1) *
( $N + 1);
}
$N = 3;
echo nthTerm( $N );
?>
|
Javascript
<script>
function nthTerm( N)
{
return N * N + (N + 1) * (N + 1);
}
let N = 3;
document.write(nthTerm(N));
</script>
|
Time Complexity: O(1)
Space Complexity: O(1) because constant variables are used
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...