Given a number (N), check if it is pentagonal or not.
Input: 12 Output: Yes Explanation: 12 is the third pentagonal number Input: 19 Output: No Explanation: The third pentagonal number is 12 while the fourth pentagonal number is 22. Hence 19 is not a pentagonal number.
Pentagonal numbers are numbers which can be arranged to form a pentagon. If N is a pentagonal number then we can use N dots or points to generate a regular pentagon (Please see figure below).
The first few pentagonal numbers are 1, 5, 12, 22, 35, 51, 70, …
Image Source: Wiki
Method I (Iterative)
We begin by noting that the nth Pentagonal Number is given by
Follow an iterative process. Consecutively substitute n = 1, 2, 3 … into the formula and store the result in some variable M. Stop, if M >= N. After iteration if M equals N then N must be a pentagonal number. Else if M exceeds N then N cannot be a pentagonal number.
function isPentagonal(N) Set i = 1 do M = (3*i*i - i)/2 i += 1 while M < N if M == N print Yes else print No
Below is the implementation of the algorithm
12 is pentagonal
The Time complexity of this method is O(n) since we need to compute successive values of pentagonal numbers up to N.
Method 2 (Efficient)
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.
Implementation of the method is given below :
19 is not pentagonal
Time and Space Complexities of this method are both O(1).
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Program to check if N is a Centered Pentagonal Number or not
- n'th Pentagonal Number
- Pentagonal Pyramidal Number
- Centered pentagonal number
- Find the sum of the first N Centered Pentagonal Number
- Program to check whether a number is Proth number or not
- Program to check if N is a Myriagon Number
- Program to check if N is a Decagonal Number
- Program to check if N is a Octadecagon number
- Program to check if N is a Octagonal Number
- Program to check if N is a Chiliagon Number
- Program to check if N is a triacontagonal number
- Program to check if N is a Icosagonal Number
- Program to check if N is a Icosidigonal Number
- Program to check if N is a Heptadecagonal Number
- Program to check if N is a Star Number
- Program to check if N is a Icositrigonal number
- Program to check if N is a Icosihenagonal number
- Program to check if N is a Hexagonal Number or not
- Program to check if N is a Icositetragonal number
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.