📜  程序检查N是否是居中的八边形数字

📅  最后修改于: 2021-05-07 00:57:16             🧑  作者: Mango

给定整数N ,任务是检查它是否是居中的八边形数。如果输出,则打印“是”,否则输出为“否”。

例子:

方法:为了解决上述问题,我们知道居中八角形数的K项为: K^{th} Term = 9*N^{2} + - 9*N + 1
因为我们必须检查给定的数字是否可以表示为居中的八边形数字。可以通过将方程式概括为以下内容进行检查:

最后,使用该公式检查计算值是否为整数,则表示N为居中的八边形数。

下面是上述方法的实现:

C++
// C++ implementation to check that
// a number is a Centered
// Octadecagonal number or not
 
#include 
using namespace std;
 
// Function to check that the
// number is a Centered
// Octadecagonal number
bool isCenteredOctadecagonal(int N)
{
 
    // Implement the formula generated
    float n = (9 + sqrt(36 * N + 45)) / 18;
 
    // Condition to check if the
    // number is a Centered
    // Octadecagonal number
    return (n - (int)n) == 0;
}
 
// Driver Code
int main()
{
    int n = 19;
 
    // Function call
    if (isCenteredOctadecagonal(n)) {
        cout << "Yes";
    }
    else {
        cout << "No";
    }
    return 0;
}


Java
// Java implementation to check that
// a number is a centered
// octadecagonal number or not
import java.lang.Math;
 
class GFG{
     
// Function to check that the
// number is a centered
// octadecagonal number
public static boolean isCenteredOctadecagonal(int N)
{
     
    // Implement the formula generated
    double n = (9 + Math.sqrt(36 * N + 45)) / 18;
     
    // Condition to check if the
    // number is a Centered
    // Octadecagonal number
    return(n - (int)n) == 0;
}
 
// Driver Code   
public static void main(String[] args)
{
    int n = 19;
 
    // Function call
    if (isCenteredOctadecagonal(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to check that
# a number is a centered octadecagonal
# number or not
import math
 
# Function to check that the
# number is a centered
# octadecagonal number
def isCenteredOctadecagonal(N):
     
    # Implement the formula generated
    n = (9 + math.sqrt(36 * N + 45)) / 18;
     
    # Condition to check if the
    # number is a centered
    # octadecagonal number
    return (n - int(n)) == 0
 
# Driver code
if __name__=='__main__':
     
    n = 19
     
    # Function call
    if isCenteredOctadecagonal(n):
        print('Yes')
    else:
        print('No')
 
# This code is contributed by rutvik_56


C#
// C# implementation to check that
// a number is a centered
// octadecagonal number or not
using System;
 
class GFG{
 
// Function to check that the
// number is a centered
// octadecagonal number
static bool isCenteredOctadecagonal(int N)
{
 
    // Implement the formula generated
    double n = (9 + Math.Sqrt(36 * N + 45)) / 18;
     
    // Condition to check if the
    // number is a Centered
    // octadecagonal number
    return (n - (int)n) == 0;
}
     
// Driver Code
static public void Main ()
{
    int n = 19;
     
    // Function call
    if (isCenteredOctadecagonal(n))
    {
        Console.Write("Yes");
    }
    else
    {
        Console.Write("No");
    }
}
}
 
//This code is contributed by ShubhamCoder


Javascript


输出:
Yes