Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Mathematical Algorithm
  • Mathematical Algorithms
  • Pythagorean Triplet
  • Fibonacci Number
  • Euclidean Algorithm
  • LCM of Array
  • GCD of Array
  • Binomial Coefficient
  • Catalan Numbers
  • Sieve of Eratosthenes
  • Euler Totient Function
  • Modular Exponentiation
  • Modular Multiplicative Inverse
  • Stein's Algorithm
  • Juggler Sequence
  • Chinese Remainder Theorem
  • Quiz on Fibonacci Numbers
Open In App
Next Article:
Centered pentagonal number
Next article icon

n’th Pentagonal Number

Last Updated : 08 May, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

Given an integer n, find the nth Pentagonal number. The first three pentagonal numbers are 1, 5, and 12 (Please see the 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
Recommended Practice
Nth Pentagonal Number
Try It!

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

 

Pentagonal Number

Below are the implementations of the above idea in different programming languages.
 

C++




// C++ program for above approach
#include<bits/stdc++.h>
using namespace std;
 
// Finding the nth pentagonal number
int pentagonalNum(int n)
{
    return (3 * n * n - n) / 2;
}
 
// Driver code
int main()
{
    int n = 10;
     
    cout << "10th Pentagonal Number is = "
         << pentagonalNum(n);
 
    return 0;
}
 
// This code is contributed by Code_Mech
 
 

C




// C program for above approach
#include <stdio.h>
#include <stdlib.h>
 
// Finding the nth Pentagonal Number
int pentagonalNum(int n)
{
    return (3*n*n - n)/2;
}
 
// Driver program to test above function
int main()
{
    int n = 10;
    printf("10th Pentagonal Number is = %d \n \n",
                             pentagonalNum(n));
 
    return 0;
}
 
 

Java




// Java program for above approach
class 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));
    }
}
 
 

Python3




# Python program for finding pentagonal numbers
def pentagonalNum( n ):
    return (3*n*n - n)/2
#Script Begins
 
n = 10
print ("10th Pentagonal Number is = ", pentagonalNum(n))
  
#Scripts Ends
 
 

C#




// C# program for above approach
using 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
// PHP program for above approach
 
// Finding the nth Pentagonal Number
function 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
?>
 
 

Javascript




<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) // since no loop or recursion is used the algorithm takes up constant time to perform the operations
Auxiliary Space: O(1) // since no extra array or data structure is used so the space taken by the algorithm is constant

Another Approach:

The formula indicates that the n-th pentagonal number depends quadratically on n. Therefore, try to find the positive integral root of N = P(n) equation. 
P(n) = nth pentagonal number 
N = Given Number
Solve for n: 
P(n) = N 
or (3*n*n – n)/2 = N 
or 3*n*n – n – 2*N = 0 … (i)
The positive root of equation (i) 
n = (1 + sqrt(24N+1))/6
After obtaining n, check if it is an integer or not. n is an integer if n – floor(n) is 0.

C++




// C++ Program to check a
// pentagonal number
#include <bits/stdc++.h>
using namespace std;
 
// Function to determine if
// N is pentagonal or not.
bool isPentagonal(int N)
{   
    // Get positive root of
    // equation P(n) = N.
    float n = (1 + sqrt(24*N + 1))/6;
     
    // Check if n is an integral
    // value of not. To get the
    // floor of n, type cast to int.
    return (n - (int) n) == 0;
}
 
// Driver Code
int main()
{
    int N = 145;   
    if (isPentagonal(N))
        cout << N << " is pentagonal " << endl;   
    else
        cout << N << " is not pentagonal" << endl;   
    return 0;
}
 
 

Java




// Java Program to check a
// pentagonal number
import java.util.*;
 
public class Main {
 
    // Function to determine if
    // N is pentagonal or not.
    public static boolean isPentagonal(int N)
    {
        // Get positive root of
        // equation P(n) = N.
        float n = (1 + (float)Math.sqrt(24 * N + 1)) / 6;
 
        // Check if n is an integral
        // value of not. To get the
        // floor of n, type cast to int.
        return (n - (int)n) == 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
 
        int N = 145;
        if (isPentagonal(N))
            System.out.println(N + " is pentagonal ");
        else
            System.out.println(N + " is not pentagonal");
    }
}
 
 

Python3




import math
 
# Function to determine if N is pentagonal or not.
def isPentagonal(N):
    # Get positive root of equation P(n) = N
    n = (1 + math.sqrt(24*N + 1))/6
     
    # Check if n is an integral value or not.
    # To get the floor of n, use the int() function
    return (n - int(n)) == 0
 
# Driver code
if __name__ == "__main__":
    N = 145
     
    if isPentagonal(N):
        print(N, "is pentagonal")
    else:
        print(N, "is not pentagonal")
 
 

C#




using System;
 
public class Program
{
 
  // Function to determine if
  // N is pentagonal or not.
  public static bool IsPentagonal(int n)
  {
    // Get positive root of equation P(n) = N.
    float x = (1 + MathF.Sqrt(24 * n + 1)) / 6;
 
    // Check if x is an integral value or not
    return MathF.Floor(x) == x;
  }
 
  public static void Main()
  {
    int n = 145;
    if (IsPentagonal(n))
      Console.WriteLine(n + " is pentagonal");
    else
      Console.WriteLine(n + " is not pentagonal");
 
    // Pause the console so that we can see the output
    Console.ReadLine();
  }
}
// This code is contributed by divyansh2212
 
 

Javascript




// js equivalent
 
// import math functions
function isPentagonal(N) {
    // Get positive root of equation P(n) = N
    let n = (1 + Math.sqrt(24 * N + 1)) / 6;
     
    // Check if n is an integral value or not.
    // To get the floor of n, use the Math.floor() function
    return (n - Math.floor(n)) === 0;
}
 
// Driver code
let N = 145;
 
if (isPentagonal(N)) {
    console.log(`${N} is pentagonal`);
} else {
    console.log(`${N} is not pentagonal`);
}
 
 
Output
145 is pentagonal 

Time Complexity: O(log n) //the inbuilt sqrt function takes logarithmic time to execute
Auxiliary Space: O(1) // since no extra array or data structure is used so the space taken by the algorithm is constant

Reference: 
https://en.wikipedia.org/wiki/Polygonal_number

 



Next Article
Centered pentagonal number

M

Mazhar Imam Khan
Improve
Article Tags :
  • DSA
  • Geometric
  • Mathematical
  • series
Practice Tags :
  • Geometric
  • Mathematical
  • series

Similar Reads

  • Centered pentagonal number
    Given an integer n, find the nth Centered pentagonal number. A Centered Pentagonal Number is a centered figurate number that represents a pentagon with a dot in the center and other dots surrounding it in pentagonal layers successively [ Source: Wiki ] Few Centered pentagonal Number are : 1, 6, 16,
    4 min read
  • Pentadecagonal Number
    Given a number N, the task is to find the Nth Pentadecagonal number. A Pentadecagonal number is a figurate number that extends the concept of triangular and square numbers to the pentadecagon(a 15-sided polygon). The Nth pentadecagonal number counts the number of dots in a pattern of N nested pentad
    3 min read
  • Second Pentagonal numbers
    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 sec
    3 min read
  • N-th polite number
    A polite number is a positive integer that can be written as the sum of two or more consecutive positive integers. Given N, find the N-th polite number.Examples: Input : 4 Output : 7 Explanation: The first 3 are 3(1+2), 5(2+3), 6(1+2+3). Input : 7 Output : 11 Explanation: 3, 5, 6, 7, 9, 10, 11. Ther
    3 min read
  • Octagonal number
    You are given a number n, the task is to find nth octagonal number. Also, find the Octagonal series till n.An octagonal number is the figure number that represent octagonal. Octagonal numbers can be formed by placing triangular numbers on the four sides of a square. Octagonal number is calculated by
    5 min read
  • Pentagonal Pyramidal Number
    Given a number n, find the nth pentagonal pyramidal number.A Pentagonal Pyramidal Number belongs to the figurate number class. It is the number of objects in a pyramid with a pentagonal base. The nth pentagonal pyramidal number is equal to sum of first n pentagonal numbers. Examples: Input : n = 3 O
    6 min read
  • Centered Pentadecagonal Number
    Given a number n, find the nth Centered Pentadecagonal Number . A Centered Pentadecagonal Number represents a dot in the center and other dots surrounding it in successive pentadecagonal(15-sided polygon) layers. Examples : Input : 2 Output : 16 Input : 8 Output : 421 n-th term of Centered pentadeca
    4 min read
  • Pentacontagon number
    Given a number N, the task is to find Nth Pentacontagon number. A Pentacontagon number is class of figurate number. It has 50 - sided polygon called pentacontagon. The N-th pentacontagon number count’s the 50 number of dots and all others dots are surrounding with a common sharing corner and make a
    3 min read
  • Pentacontahenagon Number
    Pentacontahenagon Number is a class of figurate numbers. It has a 51 sided polygon called Pentacontahenagon. The N-th Pentacontahenagon number count’s the 51 number of dots and all other dots are surrounding with a common sharing corner and make a pattern.First few Pentacontahenagonol Numbers are: 1
    3 min read
  • Tetracontadigonal Number
    Given a number N, the task is to find Nth Tetracontadigon number. A Tetracontadigon number is a class of figurate numbers. It has a 42-sided polygon called Tetracontadigon. The N-th Tetracontadigonal number count’s the 42 number of dots and all other dots are surrounding with a common sharing corner
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences