📜  程序检查N是否为同心六边形

📅  最后修改于: 2021-05-07 05:37:52             🧑  作者: Mango

给定整数N ,任务是检查N是否为同心六边形。如果数字N是同心六边形,则打印“是”,否则打印“否”

例子:

方法:

  1. 同心六角形数的第K项为:
  2. 因为我们必须检查给定的数字是否可以表示为同心六角形数字。可以检查为:
  3. 如果使用上述公式计算出的K值为整数,则N为同心六边形数。
  4. 否则,数字N不是同心六边形。

    下面是上述方法的实现:

    C++
    // C++ program to check if N is a
    // Concentric Hexagonal Number
      
    #include 
    using namespace std;
      
    // Function to check if the
    // number is a Concentric hexagonal number
    bool isConcentrichexagonal(int N)
    {
        float n = sqrt((2 * N) / 3);
      
        // Condition to check if the
        // number is a Concentric 
        // hexagonal number
        return (n - (int)n) == 0;
    }
      
    // Driver Code
    int main()
    {
        int N = 6;
      
        // Function call
        if (isConcentrichexagonal(N)) {
            cout << "Yes";
        }
        else {
            cout << "No";
        }
        return 0;
    }


    Java
    // Java program to check if N is a
    // Concentric Hexagonal Number
    class GFG{
      
    // Function to check if the
    // number is a Concentric hexagonal number
    static boolean isConcentrichexagonal(int N)
    {
        float n = (float) Math.sqrt((2 * N) / 3);
      
        // Condition to check if the
        // number is a Concentric 
        // hexagonal number
        return (n - (int)n) == 0;
    }
      
    // Driver Code
    public static void main(String[] args)
    {
        int N = 6;
      
        // Function call
        if (isConcentrichexagonal(N)) 
        {
            System.out.print("Yes");
        }
        else 
        {
            System.out.print("No");
        }
    }
    }
      
    // This code is contributed by PrinciRaj1992


    Python3
    # Python3 program to check if N is a 
    # concentric hexagonal number 
    import math
      
    # Function to check if the number
    # is a concentric hexagonal number 
    def isConcentrichexagonal(N):
          
        n = math.sqrt((2 * N) / 3)
          
        # Condition to check if the 
        # number is a concentric 
        # hexagonal number
        return (n - int(n)) == 0
      
    # Driver code
    N = 6
      
    if isConcentrichexagonal(N):
        print("Yes")
    else:
        print("No")
      
    # This code is contributed by divyeshrabadiya07


    C#
    // C# program to check if N is a
    // concentric hexagonal number
    using System;
      
    class GFG{
      
    // Function to check if the number 
    // is a concentric hexagonal number
    static bool isConcentrichexagonal(int N)
    {
        float n = (float) Math.Sqrt((2 * N) / 3);
      
        // Condition to check if the
        // number is a concentric 
        // hexagonal number
        return (n - (int)n) == 0;
    }
      
    // Driver Code
    public static void Main()
    {
        int N = 6;
      
        // Function call
        if (isConcentrichexagonal(N)) 
        {
            Console.Write("Yes");
        }
        else
        {
            Console.Write("No");
        }
    }
    }
      
    // This code is contributed by Code_Mech


    输出:
    Yes