给定整数N ,任务是检查它是否是居中的八边形数。如果输出,则打印“是”,否则输出为“否”。
Centered Octadecagonal number represents a dot in the centre and others dot are arranged around it in successive layers of octadecagon(18 sided polygon). The first few Centered Octadecagonal numbers are 1, 19, 55, 109, 181, 271, 379, …
例子:
Input: N = 19
Output: Yes
Explanation:
19 is the Second Centered Octadecagonal number is 19.
Input: 38
Output: No
Explanation:
38 is not a Centered Octadecagonal number.
方法:为了解决上述问题,我们知道居中八角形数的K个项为:
因为我们必须检查给定的数字是否可以表示为居中的八边形数字。可以通过将方程式概括为以下内容进行检查:
=>
=>
最后,使用该公式检查计算值是否为整数,则表示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