📌  相关文章
📜  程序检查N是否是一个十进制数

📅  最后修改于: 2021-04-21 23:16:55             🧑  作者: Mango

给定一个数字N ,任务是检查N是否为一个Enneadecagonal Number。如果数字N是一个九边形数字,则打印“是”,否则打印“否”

例子:

方法:

1.十边形数的第K项为
K^{th} Term = \frac{17*K^{2} - 15*K}{2}

2.由于我们必须检查给定的数字是否可以表示为Enneadecagonal Number 。可以检查为:

3.如果使用上述公式计算出的K的值为整数,则N为Enneadecagonal Number。

4.其他N不是一个十角形数字。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if number N
// is a enneadecagonal number
bool isenneadecagonal(int N)
{
    float n
        = (15 + sqrt(136 * N + 225))
          / 34;
 
    // Condition to check if N is a
    // enneadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    // Given Number
    int N = 19;
 
    // Function call
    if (isenneadecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if number N
// is a enneadecagonal number
static boolean isenneadecagonal(int N)
{
    float n = (float)(15 + Math.sqrt(136 * N +
                                     225)) / 34;
 
    // Condition to check if N is a
    // enneadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Number
    int N = 19;
 
    // Function call
    if (isenneadecagonal(N))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by rutvik_56


Python3
# Python3 program for the above approach
import math
 
# Function to check if number N
# is a enneadecagonal number
def isenneadecagonal(N):
 
    n = (15 + math.sqrt(136 * N + 225)) / 34
 
    # Condition to check if N is a
    # enneadecagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 19
 
# Function call
if (isenneadecagonal(N)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by divyamohan123


C#
// C# program for the above approach
using System;
 
class GFG{
 
// Function to check if number N
// is a enneadecagonal number
static bool isenneadecagonal(int N)
{
    float n = (float)(15 + Math.Sqrt(136 * N +
                                     225)) / 34;
     
    // Condition to check if N is a
    // enneadecagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
         
    // Given number
    int N = 19;
     
    // Function call
    if (isenneadecagonal(N))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by shubhamsingh10


Javascript


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)