📜  程序检查N是否为二十六面体数字

📅  最后修改于: 2021-04-27 23:41:33             🧑  作者: Mango

给定整数N ,任务是检查它是否为二十六边形数字。

例子:

方法:

  1. 二十进制六边形数的第K项为
    K^{th} Term = \frac{24*K^{2} - 22*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为二十面体六边形。可以检查如下–
  1. 最后,检查使用此公式计算出的值是否为整数,这意味着N为二十六边形数字。

下面是上述方法的实现:

C++
// C++ program to check whether
// a number is a icosihexagonal number
// or not
 
#include 
 
using namespace std;
 
// Function to check whether the
// number is a icosihexagonal number
bool isicosihexagonal(int N)
{
    float n
        = (22 + sqrt(192 * N + 484))
          / 48;
 
    // Condition to check if the
    // number is a icosihexagonal number
    return (n - (int)n) == 0;
}
 
// Driver code
int main()
{
    int i = 26;
 
    if (isicosihexagonal(i)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program to check whether the
// number is a icosihexagonal number
// or not
class GFG{
 
// Function to check whether the
// number is a icosihexagonal number
static boolean isicosihexagonal(int N)
{
    float n = (float) ((22 + Math.sqrt(192 * N +
                                       484)) / 48);
     
    // Condition to check if the number
    // is a icosihexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 26;
     
    // Function call
    if (isicosihexagonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by shubham


Python3
# Python3 program to check whether
# a number is a icosihexagonal number
# or not
import numpy as np
 
# Function to check whether the
# number is a icosihexagonal number
def isicosihexagonal(N):
 
    n = (22 + np.sqrt(192 * N + 484)) / 48
 
    # Condition to check if the
    # number is a icosihexagonal number
    return (n - (int(n))) == 0
 
# Driver code
i = 26
 
if (isicosihexagonal(i)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by PratikBasu


C#
// C# program to check whether the
// number is a icosihexagonal number
// or not
using System;
class GFG{
 
// Function to check whether the
// number is a icosihexagonal number
static bool isicosihexagonal(int N)
{
    float n = (float)((22 + Math.Sqrt(192 * N +
                                      484)) / 48);
     
    // Condition to check if the number
    // is a icosihexagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main()
{
     
    // Given number
    int N = 26;
     
    // Function call
    if (isicosihexagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Code_Mech


Javascript


输出:
Yes