📜  程序检查N是否为五角数

📅  最后修改于: 2021-05-05 02:17:58             🧑  作者: Mango

给定一个数字(N),检查它是否为五边形。
例子 :

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.

五边形数字是可以排列成五边形的数字。如果N是五边形,则可以使用N个点或点来生成规则的五边形(请参见下图)。
前几个五边形数字是1、5、12、22、35、51、70,…

图片来源:Wiki
方法I(迭代)
我们首先注意到第n个五角数为
P_n = \frac{3*n^2-n}{2}
遵循一个迭代过程。连续地将n = 1,2,3…代入公式并将结果存储在某个变量M中。如果M> = N,则停止。在迭代之后,如果M等于N,则N必须为五角形数。否则,如果M超过N,则N不能为五角形数。
算法

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

下面是算法的实现

C++
// C++ program to check
// pentagonal numbers.
#include 
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;
}


Java
// 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.


Python3
# 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.


C#
// 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.


PHP


Javascript


C++
// C++ Program to check a
// pentagonal number
#include 
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;
}


Java
// 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.


Python3
# 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'


C#
// 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.


PHP


Javascript


输出:

12 is pentagonal 

该方法的时间复杂度为O(n),因为我们需要计算不超过N的五边形数的连续值。方法2(高效)
该公式表明,第n个五边形数二次取决于n。因此,尝试找到N = P(n)方程的正整数根。
P(n)=第n个五边形数
N =给定数
解决n:
P(n)= N
或(3 * n * n – n)/ 2 = N
或3 * n * n – n – 2 * N = 0…(i)
等式(i)的正根
n =(1 +平方(24N + 1))/ 6
获得n后,检查它是否为整数。如果n – floor(n)为0,则n为整数。
该方法的实现如下:

C++

// C++ Program to check a
// pentagonal number
#include 
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;
}

Java

// 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.

Python3

# 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'

C#

// 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.

的PHP


Java脚本


输出 :

19 is not pentagonal

此方法的时间和空间复杂度均为O(1)。
参考 :
1)维基百科-五角形数字
2)Wolfram Alpha –五角形数字