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

📅  最后修改于: 2021-04-24 04:53:16             🧑  作者: Mango

给定整数N ,任务是检查N是否为中心十进制数。如果数字N是居中的十进制数字,则打印“是”,否则打印“否”

例子:

方法:

1.中心十进制数的第K项为
K^{th} Term = 4*K^{2} - 4*K + 1

2.由于我们必须检查给定的数字是否可以表示为中心十进制数。可以检查如下:

3.如果使用上述公式计算的K值为整数,则N为中心十进制数。

4.否则,数字N不是中心十进制数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if number N
// is a Centered decagonal number
bool isCentereddecagonal(int N)
{
    float n
        = (5 + sqrt(20 * N + 5))
          / 10;
 
    // Condition to check if N
    // is Centered Decagonal Number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int N = 11;
 
    // Function call
    if (isCentereddecagonal(N)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java implementation to check that a number
// is a centered decagonal number or not
import java.lang.Math;
 
class GFG{
     
// Function to check that the number
// is a centered decagonal number
public static boolean isCentereddecagonal(int N)
{
    double n = (5 + Math.sqrt(20 * N + 5)) / 10;
 
    // Condition to check if the number
    // is a centered decagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 11;
 
    // Function call
    if (isCentereddecagonal(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by ShubhamCoder


Python3
# Python3 program for the above approach
import numpy as np
 
# Function to check if the number N
# is a centered decagonal number
def isCentereddecagonal(N):
 
    n = (5 + np.sqrt(20 * N + 5)) / 10
 
    # Condition to check if N
    # is centered decagonal number
    return (n - int(n)) == 0
 
# Driver Code
N = 11
 
# Function call
if (isCentereddecagonal(N)):
    print ("Yes")
else:
    print ("No")
 
# This code is contributed by PratikBasu


C#
// C# implementation to check that a number
// is a centered decagonal number or not
using System;
 
class GFG{
     
// Function to check that the number
// is a centered decagonal number
static bool isCentereddecagonal(int N)
{
    double n = (5 + Math.Sqrt(20 * N + 5)) / 10;
     
    // Condition to check if the number
    // is a centered decagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
    int n = 11;
     
    // Function call
    if (isCentereddecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
// This code is contributed by ShubhamCoder


Javascript


输出:
Yes

时间复杂度: O(1)

辅助空间: O(1)