📜  程序检查N是否为余角数

📅  最后修改于: 2021-04-26 19:22:02             🧑  作者: Mango

给定一个整数N ,任务是检查它是否是一个余弦数。如果数字N是余角数字,则打印“是”,否则打印“否”

例子:

方法:

1.在K的20边数术语被给定为
K^{th} Term = \frac{18*K^{2} - 16*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 icosagonal number
bool iicosagonal(int N)
{
    float n
        = (16 + sqrt(144 * N + 256))
          / 36;
 
    // Condition to check if the
    // N is a icosagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 20;
 
    // Function call
    if (iicosagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to check if the number
// N is a icosagonal number
static boolean iicosagonal(int N)
{
    float n = (float)((16 + Math.sqrt(144 * N +
                                      256)) / 36);
 
    // Condition to check if the
    // N is a icosagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Number
    int N = 20;
 
    // Function call
    if (iicosagonal(N))
    {
        System.out.print("Yes");
    }
    else
    {
        System.out.print("No");
    }
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 program for the above approach
import numpy as np
 
# Function to check if the number
# N is a icosagonal number
def iicosagonal(N):
 
    n = (16 + np.sqrt(144 * N + 256)) / 36
 
    # Condition to check if the
    # N is a icosagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 20
 
# Function call
if (iicosagonal(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 icosagonal number
static bool iicosagonal(int N)
{
    float n = (float)((16 + Math.Sqrt(144 * N +
                                      256)) / 36);
                                       
    // Condition to check if the
    // N is a icosagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void Main(string[] args)
{
     
    // Given Number
    int N = 20;
 
    // Function call
    if (iicosagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by rutvik_56


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)