📜  程序检查N是否是居中的五边形数字

📅  最后修改于: 2021-05-06 10:24:42             🧑  作者: Mango

给定数字N ,任务是检查N是否为居中的五边形数字。如果数字N是居中的五边形数字,则打印“是”,否则打印“否”

例子:

方法:

1.中心五角形数的第K项为:
K^{th} Term = \frac{15*K^{2} - 15*K + 2}{2}

2.由于我们必须检查给定的数字是否可以表示为中心五角形数。可以检查为:

3.如果使用上述公式计算出的K的值为整数,则N为中心五角形数。

4.否则,数字N不是居中的五边形数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if number N is a
// Centered Pentadecagonal Number
bool isCenteredpentadecagonal(int N)
{
    float n
        = (16 + sqrt(120 * N + 105))
          / 30;
 
    // Condition to check if N is a
    // Centered Pentadecagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 16;
 
    // Function call
    if (isCenteredpentadecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if number N is a
// Centered Pentadecagonal Number
static boolean isCenteredpentadecagonal(int N)
{
    float n = (float)(16 + Math.sqrt(120 * N +
                                     105)) / 30;
 
    // Condition to check if N is a
    // Centered Pentadecagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Number
    int N = 16;
 
    // Function call
    if (isCenteredpentadecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
import math
 
# Function to check if number N is a
# centered pentadecagonal number
def isCenteredpentadecagonal(N):
     
    n = (16 + math.sqrt(120 * N + 105)) / 30
     
    # Condition to check if N is a
    # centered pentadecagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 16
 
# Function call
if isCenteredpentadecagonal(N):
    print("Yes")
else :
    print("No")
 
# This code is contributed by ishayadav181


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to check if number N is a
// centered pentadecagonal number
public static bool isCenteredpentadecagonal(int N)
{
    double n = (16 + Math.Sqrt(120 * N +
                               105)) / 30;
     
    // Condition to check if N is a
    // centered pentadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver code
public static void Main()
{
     
    // Given number
    int N = 16;
     
    // Function call
    if (isCenteredpentadecagonal(N))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:
No

时间复杂度: O(1)

辅助空间: O(1)