n'th Pentagonal Number - GeeksforGeeks

25 Jan, 2022
Given an integer n, find the nth Pentagonal number. The first three pentagonal numbers are 1, 5, and 12 (Please see below diagram). The n’th pentagonal number Pn is the number of distinct dots in a pattern of dots consisting of the outlines of regular pentagons with sides up to n dots when the pentagons are overlaid so that they share one vertex [Source Wiki]Examples :  
Input: n = 1
Output: 1

Input: n = 2
Output: 5

Input: n = 3
Output: 12
 
In general, a polygonal number (triangular number, square number, etc) is a number represented as dots or pebbles arranged in the shape of a regular polygon. The first few pentagonal numbers are: 1, 5, 12, etc. If s is the number of sides in a polygon, the formula for the nth s-gonal number P (s, n) is  
nth s-gonal number P(s, n) = (s - 2)n(n-1)/2 + n

If we put s = 5, we get

n'th Pentagonal number Pn = 3*n*(n-1)/2 + n
Examples:  
Pentagonal Number
 
Below are the implementations of the above idea in different programming languages. 
C++
C
Java
Python3
C#
PHP
Javascript
// C++ program for above approach#include<bits/stdc++.h>using namespace std; // Finding the nth pentagonal numberint pentagonalNum(int n){    return (3 * n * n - n) / 2;} // Driver codeint main(){    int n = 10;         cout << "10th Pentagonal Number is = "         << pentagonalNum(n);     return 0;} // This code is contributed by Code_Mech
// C program for above approach#include <stdio.h>#include <stdlib.h> // Finding the nth Pentagonal Numberint pentagonalNum(int n){    return (3*n*n - n)/2;} // Driver program to test above functionint main(){    int n = 10;    printf("10th Pentagonal Number is = %d \n \n",                             pentagonalNum(n));     return 0;}
// Java program for above approachclass Pentagonal{    int pentagonalNum(int n)    {        return (3*n*n - n)/2;    }} public class GeeksCode{    public static void main(String[] args)    {        Pentagonal obj = new Pentagonal();        int n = 10;           System.out.printf("10th petagonal number is = "                          + obj.pentagonalNum(n));    }}
# Python program for finding pentagonal numbersdef pentagonalNum( n ):    return (3*n*n - n)/2#Script Begins n = 10print ("10th Pentagonal Number is = ", pentagonalNum(n))  #Scripts Ends
// C# program for above approachusing System; class GFG {         static int pentagonalNum(int n)    {        return (3 * n * n - n) / 2;    }     public static void Main()    {        int n = 10;                 Console.WriteLine("10th petagonal"        + " number is = " + pentagonalNum(n));    }} // This code is contributed by vt_m.
<?php// PHP program for above approach // Finding the nth Pentagonal Numberfunction pentagonalNum($n){    return (3 * $n * $n - $n) / 2;} // Driver Code$n = 10;echo "10th Pentagonal Number is = ",                  pentagonalNum($n); // This code is contributed by ajit?>
<script> // Javascript program for above approach     function pentagonalNum(n)    {        return (3 * n * n - n) / 2;    } // Driver code to test above methods         let n = 10;                   document.write("10th petagonal"        + " number is = " + pentagonalNum(n));                   // This code is contributed by avijitmondal1998.</script>
Output :  
10th Pentagonal Number is = 145
Time Complexity: O(1)Auxiliary Space: O(1)Reference: https://en.wikipedia.org/wiki/Polygonal_numberThis article is contributed by Mazhar Imam Khan. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
Code_Mech
avijitmondal1998
manikarora059
amartyaghoshgfg
series
Geometric
Mathematical
Mathematical
series
Geometric
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Circle and Lattice Points
Program for distance between two points on earth
Convex Hull | Set 1 (Jarvis's Algorithm or Wrapping)
Queries on count of points lie inside a circle
Convex Hull | Set 2 (Graham Scan)
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7