Second Pentagonal numbers
Last Updated :
23 Sep, 2022
The second pentagonal numbers are a collection of objects which can be arranged in the form of a regular pentagon.
Second Pentagonal series is:
2, 7, 15, 26, 40, 57, 77, 100, 126, …..
Find the Nth term of the Second Pentagonal Series
Given an integer N. The task is to find the N-th term of the second pentagonal series.
Examples:
Input: N = 1
Output: 2
Input: N = 4
Output: 26
Approach: The idea is to find the general term of the series which can be computed with the help of the following observations as below:
Series = 2, 7, 15, 26, 40, 57, 77, 100, 126, …..
Difference = 7 – 2, 15 – 7, 26 – 15, 40 – 26, …………….
= 5, 8, 11, 14……which is an AP
So nth term of given series
nth term = 2 + (5 + 8 + 11 + 14 …… (n-1)terms)
= 2 + (n-1)/2*(2*5+(n-1-1)*3)
= 2 + (n-1)/2*(10+3n-6)
= 2 + (n-1)*(3n+4)/2
= n*(3*n + 1)/2
Therefore, the Nth term of the series is given as
Below is the implementation of the above approach:
C++
#include <iostream>
#include <math.h>
using namespace std;
void findNthTerm( int n)
{
cout << n * (3 * n + 1) / 2
<< endl;
}
int main()
{
int N = 4;
findNthTerm(N);
return 0;
}
|
Java
class GFG{
static void findNthTerm( int n)
{
System.out.print(n * ( 3 *
n + 1 ) / 2 + "\n" );
}
public static void main(String[] args)
{
int N = 4 ;
findNthTerm(N);
}
}
|
Python3
def findNthTerm(n):
print (n * ( 3 * n + 1 ) / / 2 , end = " " );
N = 4 ;
findNthTerm(N);
|
C#
using System;
class GFG{
static void findNthTerm( int n)
{
Console.Write(n * (3 *
n + 1) / 2 + "\n" );
}
public static void Main()
{
int N = 4;
findNthTerm(N);
}
}
|
Javascript
<script>
function findNthTerm(n)
{
document.write(n * (3 * n + 1) / 2);
}
N = 4;
findNthTerm(N);
</script>
|
Time Complexity: O(1)
Auxiliary space: O(1)
Reference: https://oeis.org/A005449
Share your thoughts in the comments
Please Login to comment...