📜  程序检查N是否为对角线数

📅  最后修改于: 2021-04-22 06:45:14             🧑  作者: Mango

给定整数N ,任务是检查N是否为Chiliagon Number。如果数字N是对角线数字,则打印“是”,否则打印“否”。

例子:

方法:

  1. Chiliagon数的第K项为
    K^{th} Term =  \frac{998*K^{2} - 996*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为Chiliagon数。可以检查如下:
  3. 如果使用上述公式计算出的K的值为整数,则N为Chiliagon数。
  4. 其他N不是对角线数字。

下面是上述方法的实现:

C++
// C++ for the above approach
#include 
using namespace std;
  
// Function to check that if N is
// Chiliagon Number or not
bool is_Chiliagon(int N)
{
    float n
        = (996 + sqrt(7984 * N + 992016))
          / 1996;
  
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int)n) == 0;
}
  
// Driver Code
int main()
{
    // Given Number
    int N = 1000;
  
    // Function call
    if (is_Chiliagon(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
class GFG{
      
// Function to check that if N is
// Chiliagon Number or not
static boolean is_Chiliagon(int N) 
{
    float n = (float)(996 + Math.sqrt(7984 * N + 
                                      992016)) / 1996;
  
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int) n) == 0;
}
  
// Driver Code
public static void main(String s[]) 
{
    // Given Number
    int N = 1000;
  
    // Function call
    if (is_Chiliagon(N)) 
    {
        System.out.print("Yes");
    } 
    else 
    {
        System.out.print("No");
    }
}
}
  
// This code is contributed by rutvik_56


Python3
# Python3 for the above approach
import math;
  
# Function to check that if N is
# Chiliagon Number or not
def is_Chiliagon(N):
  
    n = (996 + math.sqrt(7984 * N + 
                         992016)) // 1996;
  
    # Condition to check if N is a
    # Chiliagon Number
    return (n - int(n)) == 0;
  
# Driver Code
  
# Given Number
N = 1000;
  
# Function call
if (is_Chiliagon(N)):
    print("Yes");
else:
    print("No");
  
# This code is contributed by Code_Mech


C#
// C# program for the above approach
using System;
class GFG{
      
// Function to check that if N is
// Chiliagon Number or not
static bool is_Chiliagon(int N) 
{
    float n = (float)(996 + Math.Sqrt(7984 * N + 
                                      992016)) / 1996;
  
    // Condition to check if N is a
    // Chiliagon Number
    return (n - (int) n) == 0;
}
  
// Driver Code
public static void Main() 
{
    // Given Number
    int N = 1000;
  
    // Function call
    if (is_Chiliagon(N)) 
    {
        Console.Write("Yes");
    } 
    else
    {
        Console.Write("No");
    }
}
}
  
// This code is contributed by Code_Mech


输出:
Yes