Find the Nth term of the series 3,10,21,36,55…
Given a positive integer N, the task is to find the Nth term of the series
3, 10, 21, 36, 55…till N terms
Examples:
Input: N = 4
Output: 36
Input: N = 6
Output: 78
Approach:
From the given series, find the formula for Nth term-
1st term = 1 * ( 2(1) + 1 ) = 3
2nd term = 2 * ( 2(2) + 1 ) = 10
3rd term = 3 * ( 2(3) + 1 ) = 21
4th term = 4 * ( 2(4) + 1 ) = 36
.
.
Nth term = N * ( 2(N) + 1 )
The Nth term of the given series can be generalized as-
TN = N * ( 2(N) + 1 )
Illustration:
Input: N = 10
Output: 210
Explanation:
TN = N * ( 2(N) + 1 )
= 10 * ( 2(10) + 1 )
= 210
Below is the implementation of the above approach-
C++
#include <iostream>
using namespace std;
int nTh( int n)
{
return n * (2 * n + 1);
}
int main()
{
int N = 10;
cout << nTh(N) << endl;
return 0;
}
|
C
#include <stdio.h>
int nTh( int n)
{
return n * (2 * n + 1);
}
int main()
{
int N = 10;
printf ( "%d" , nTh(N));
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
int N = 10 ;
System.out.println(nTh(N));
}
public static int nTh( int n)
{
return n * ( 2 * n + 1 );
}
}
|
Python
def nTh(n):
return n * ( 2 * n + 1 )
N = 10
print (nTh(N))
|
C#
using System;
public class GFG
{
public static int nTh( int n)
{
return n * (2 * n + 1);
}
static public void Main (){
int N = 10;
Console.Write(nTh(N));
}
}
|
Javascript
<script>
function nTh(n) {
return n * (2 * n + 1);
}
let N = 10;
document.write(nTh(N) + '<br>' );
</script>
|
Time Complexity: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant
Last Updated :
19 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...