Program to check if N is a Pentagonal Number

Given a number (N), check if it is pentagonal or not.
Examples :

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
 P_n = \frac{3*n^2-n}{2}

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.

Algorithm

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

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to check
// pentagonal numbers.
#include <iostream>
using namespace std;
  
// Function to determine
// if N is pentagonal or not.
bool isPentagonal(int N) 
{
    int i = 1, M;
      
    do {
  
        // Substitute values of i
        // in the formula.
        M = (3*i*i - i)/2;
        i += 1;
    }
    while ( M < N );
      
    return (M == N);
}
  
// Driver Code
int main()
{
    int N = 12;
      
    if (isPentagonal(N)) 
        cout << N << " is pentagonal " << endl;    
    else
        cout << N << " is not pentagonal" << endl;
      
    return 0;

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check
// pentagonal numbers.
import java.io.*;
  
class GFG {
      
// Function to determine
// if N is pentagonal or not.
static Boolean isPentagonal(int N) 
{
    int i = 1, M;
       
    do {
   
        // Substitute values of
        // i in the formula.
        M = (3*i*i - i)/2;
        i += 1;
    }
    while ( M < N );
       
    return (M == N);
}
    public static void main (String[] args) {
    int N = 12;
       
    if (isPentagonal(N)) 
        System.out.println( N + " is pentagonal " );    
    else
        System.out.println( N + " is not pentagonal");
  
    }
}
  
// This code is contributed by Gitanjali.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# python3 program to check
# pentagonal numbers.
import math 
  
# Function to determine if
# N is pentagonal or not.
def isPentagonal( N ) :
  
    i = 1
    while True:
  
        # Substitute values of i
        # in the formula.
        M = (3 * i * i - i) / 2
        i += 1
      
        if ( M >= N ):
            break
      
    return (M == N)
      
# Driver method
N = 12
if (isPentagonal(N)):
    print(N , end = ' ')
    print ("is pentagonal "
else:
    print (N , end = ' ')
    print ("is not pentagonal")
  
# This code is contributed by Gitanjali.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check pentagonal numbers.
using System;
  
class GFG {
      
// Function to determine
// if N is pentagonal or not.
static bool isPentagonal(int N) 
{
    int i = 1, M;
      
    do {
  
        // Substitute values of
        // i in the formula.
        M = (3 * i * i - i) / 2;
        i += 1;
    }
    while ( M < N );
      
    return (M == N);
}
  
// Driver Code
public static void Main () 
{
    int N = 12;
      
    if (isPentagonal(N)) 
    Console.Write( N + " is pentagonal " ); 
    else
    Console.Write( N + " is not pentagonal");
  
}
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to check
// pentagonal numbers.
  
// Function to determine
// if N is pentagonal or not.
function isPentagonal(int $N
{
    $i = 1; 
    $M;
      
    do {
  
        // Substitute values of i
        // in the formula.
        $M = (3 * $i * $i - $i) / 2;
        $i += 1;
    }
    while ($M < $N);
      
    return ($M == $N);
}
  
    // Driver Code
    $N = 12;
      
    if (isPentagonal($N)) 
        echo $N , " is pentagonal "
    else
        echo $N ," is not pentagonal" ;
      
// This code is contributed by anuj_67.
?>

chevron_right



Output:

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 :

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = 19;    
    if (isPentagonal(N)) 
        cout << N << " is pentagonal " << endl;    
    else
        cout << N << " is not pentagonal" << endl;    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to check
// pentagonal numbers.
import java.io.*;
  
class GFG {
      
// Function to determine if
// N is pentagonal or not.
static Boolean isPentagonal(int N) 
{
        // Get positive root of
    // equation P(n) = N.
    double n = (1 + 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;
}
    public static void main (String[] args) {
    int N = 19;
       
    if (isPentagonal(N)) 
        System.out.println( N + " is pentagonal " );    
    else
        System.out.println( N + " is not pentagonal");
  
    }
}
  
// This code is contributed by Gitanjali.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 code Program to  
# check a pentagonal number
  
# Import math library
import math as m
  
# Function to determine if
# N is pentagonal or not
def isPentagonal( n ):
      
    # Get positive root of
    # equation P(n) = N.
    n = (1 + m.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
N = 19
  
if (isPentagonal(N)):
    print ( N, " is pentagonal "
else:
    print ( N, " is not pentagonal"
  
# This code is contributed by 'saloni1297'

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to check pentagonal numbers.
using System;
  
class GFG {
  
    // Function to determine if
    // N is pentagonal or not.
    static bool isPentagonal(int N)
    {
        // Get positive root of
        // equation P(n) = N.
        double n = (1 + 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()
    {
        int N = 19;
  
        if (isPentagonal(N))
            Console.Write(N + " is pentagonal ");
        else
            Console.Write(N + " is not pentagonal");
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP Program to check
// a pentagonal number
  
// Function to determine if
// N is pentagonal or not.
function isPentagonal($N)
    // Get positive root of
    // equation P(n) = N.
    $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
$N = 19; 
if (isPentagonal($N)) 
    echo $N . " is pentagonal "
else
    echo $N . " is not pentagonal";
  
// This code is contributed by mits.
?>

chevron_right



Output :

19 is not pentagonal

Time and Space Complexities of this method are both O(1).

References :
1) Wikipedia – Pentagonal Numbers
2) Wolfram Alpha – Pentagonal Numbers



My Personal Notes arrow_drop_up

Intern at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : vt_m, Mithun Kumar