📜  程序检查N是否为居中非十进制数

📅  最后修改于: 2021-04-29 04:06:16             🧑  作者: Mango

给定整数N ,任务是检查它是否是居中的非十进制数字。

例子:

方法:要解决上述问题,我们必须知道中心非十进制数的K项为: K^{th} Term = \frac {19*N^{2} - 19*N + 2}{2}
因为我们必须检查给定的数字是否可以表示为中心非十进制数字。可以通过将公式推广为:

最后,使用此公式检查计算值是否为整数,如果为整数,则表示N为中心非十进制数。
下面是上述方法的实现:

C++
// C++ implementation to check that
// a number is a Centered
// nonadecagonal number or not
 
#include 
using namespace std;
 
// Function to check that the
// number is a Centered
// nonadecagonal number
bool isCenterednonadecagonal(int N)
{
    float n = (19
               + sqrt(152 * N + 209))
              / 38;
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java implementation to check that
// a number is a Centered
// nonadecagonal number or not
class GFG{
 
// Function to check that the
// number is a Centered
// nonadecagonal number
static boolean isCenterednonadecagonal(int N)
{
    float n = (float)((19 + Math.sqrt(152 * N +
                                      209)) / 38);
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation to check that
# a number is a Centered
# nonadecagonal number or not
import math
 
# Function to check that the
# number is a Centered
# nonadecagonal number
def isCenterednonadecagonal(N):
 
    n = (19 + math.sqrt(152 * N +
                        209)) / 38;
 
    # Condition to check if the
    # number is a Centered
    # nonadecagonal number
    return (n - int(n)) == 0;
 
# Driver Code
n = 20;
 
# Function call
if (isCenterednonadecagonal(n)):
    print("Yes");
 
else:
    print("No");
 
# This code is contributed by Code_Mech


C#
// C# implementation to check that
// a number is a Centered
// nonadecagonal number or not
using System;
class GFG{
 
// Function to check that the
// number is a Centered
// nonadecagonal number
static bool isCenterednonadecagonal(int N)
{
    float n = (float)((19 + Math.Sqrt(152 * N +
                                      209)) / 38);
 
    // Condition to check if the
    // number is a Centered
    // nonadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main()
{
    int n = 20;
 
    // Function call
    if (isCenterednonadecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by Nidhi_biet


Javascript


输出:
Yes