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: 2Input: 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++
// C++ implementation to // find N-th term in the series #include <iostream> #include <math.h> using namespace std; // Function to find N-th term // in the series void findNthTerm( int n) { cout << n * (3 * n + 1) / 2 << endl; } // Driver code int main() { int N = 4; findNthTerm(N); return 0; } |
Java
// Java implementation to // find N-th term in the series class GFG{ // Function to find N-th term // in the series static void findNthTerm( int n) { System.out.print(n * ( 3 * n + 1 ) / 2 + "\n" ); } // Driver code public static void main(String[] args) { int N = 4 ; findNthTerm(N); } } // This code is contributed by 29AjayKumar |
Python3
# Python3 implementation to # find N-th term in the series # Function to find N-th term # in the series def findNthTerm(n): print (n * ( 3 * n + 1 ) / / 2 , end = " " ); # Driver code N = 4 ; findNthTerm(N); # This code is contributed by Code_Mech |
C#
// C# implementation to // find N-th term in the series using System; class GFG{ // Function to find N-th term // in the series static void findNthTerm( int n) { Console.Write(n * (3 * n + 1) / 2 + "\n" ); } // Driver code public static void Main() { int N = 4; findNthTerm(N); } } // This code is contributed by Code_Mech |
Javascript
<script> // Javascript implementation t // find N-th term in the series // Function to find N-th term // in the series function findNthTerm(n) { document.write(n * (3 * n + 1) / 2); } // Driver code N = 4; findNthTerm(N); </script> |
26
Time Complexity: O(1)
Auxiliary space: O(1)
Reference: https://oeis.org/A005449
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!