📜  程序检查N是否为正二十进制数

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

给定整数N ,任务是检查它是否为正二十进制数。如果数字N是正二十进制数字,则打印“是”,否则打印“否”。

例子:

方法:

1.在KIcosidigonal数术语被给定为
K^{th} Term = \frac{20*K^{2} - 18*K}{2}

2.由于我们必须检查给定的数字是否可以表示为正二十进制数。可以按以下方式检查–

3.如果使用上式计算的K值为整数,则N为正二十进制数。

4.其他N不是二十进制的数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the number N
// is a Icosidigonal number
bool isIcosidigonal(int N)
{
    float n
        = (18 + sqrt(160 * N + 324))
          / 40;
 
    // Condition to check if the
    // number is a Icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if the number N
// is a icosidigonal number
static boolean isIcosidigonal(int N)
{
    float n = (float) ((18 + Math.sqrt(160 * N +
                                       324)) / 40);
 
    // Condition to check if the number
    // is a icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program for the above approach
import numpy as np
 
# Function to check if the number N
# is a icosidigonal number
def isIcosidigonal(N):
 
    n = (18 + np.sqrt(160 * N + 324)) / 40
 
    # Condition to check if N
    # is a icosidigonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 22
 
# Function call
if (isIcosidigonal(N)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by PratikBasu


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if the number N
// is a icosidigonal number
static bool isIcosidigonal(int N)
{
    float n = (float) ((18 + Math.Sqrt(160 * N +
                                    324)) / 40);
 
    // Condition to check if the number
    // is a icosidigonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given number
    int N = 22;
 
    // Function call
    if (isIcosidigonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)