给定一个数字N ,任务是检查N是否为一个Enneadecagonal Number。如果数字N是一个九边形数字,则打印“是”,否则打印“否” 。
Enneadecagonal Number is a 19-sided polygon in mathematics. It belongs to a class of figurative number. The number contains the number of dots and the dots are arranged in a pattern or series. An Enneadecagonal Number is also known as Nonadecagon. The dots have common points and all other dots are arrange in the successive layer. The nth Enneadecagonal Number counts the seventeen number of dots and all the other dots are surrounding with a common sharing corner and make a pattern. The first few Enneadecagonal Numbers are 1, 19, 54, 106, 175…
例子:
Input: N = 19
Output: Yes
Explanation:
Second Enneadecagonal Number is 19.
Input: N = 30
Output: No
方法:
1.十边形数的第K个项为
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)