给定整数N ,任务是检查N是否为Chiliagon Number。如果数字N是对角线数字,则打印“是”,否则打印“否”。
Chiliagon Number is class of figurate number. It has 1000 – sided polygon called Chiliagon. The N-th Chiliagon Number counts the 1000 number of dots and all others dots are surrounding with a common sharing corner and make a pattern. The first few Chiliagon Numbers are 1, 1000, 2997, 5992, …
例子:
Input: N = 1000
Output: Yes
Explanation:
Second chiliagon number is 1000
Input: 35
Output: No
方法:
- Chiliagon数的第K个项为
- 因为我们必须检查给定的数字是否可以表示为Chiliagon数。可以检查如下:
=>
=> - 如果使用上述公式计算出的K的值为整数,则N为Chiliagon数。
- 其他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