给定整数N ,任务是检查它是否是七边形数。如果数字N是七边形数字,则打印“是”,否则打印“否” 。
Heptadecagonal Number is class of figurate number. It has 17-sided polygon called heptadecagon. The N-th heptadecagonal number counts the seventeen number of dots and all others dots are surrounding with a common sharing corner and make a pattern. The first few heptadecagonal numbers are 1, 17, 48, 94, 155, 231…
例子:
Input: N = 17
Output: Yes
Explanation:
Second heptadecagonal number is 17.
Input: N = 30
Output: No
方法:
1.七边形数的第K个项为
2.由于我们必须检查给定的数字是否可以表示为七边形数。可以按以下方式检查–
=>
=>
3.如果使用上述公式计算出的K的值为整数,则N为七边形数。
4.其他N不是七边形数。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if the number N
// is a heptadecagonal number
bool isheptadecagonal(int N)
{
float n
= (13 + sqrt(120 * N + 169))
/ 30;
// Condition to check if number N
// is a heptadecagonal number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 17;
// Function call
if (isheptadecagonal(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to check if the number N
// is a heptadecagonal number
static boolean isheptadecagonal(int N)
{
float n = (float) ((13 + Math.sqrt(120 * N +
169)) / 30);
// Condition to check if number N
// is a heptadecagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
// Given Number
int N = 17;
// Function call
if (isheptadecagonal(N))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
import numpy as np
# Function to check if the number N
# is a heptadecagonal number
def isheptadecagonal(N):
n = (13 + np.sqrt(120 * N + 169)) / 30
# Condition to check if number N
# is a heptadecagonal number
return (n - int(n)) == 0
# Driver Code
N = 17
# Function call
if (isheptadecagonal(N)):
print("Yes")
else:
print("No")
# This code is contributed by PratikBasu
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if the number N
// is a heptadecagonal number
static bool isheptadecagonal(int N)
{
float n = (float) ((13 + Math.Sqrt(120 * N +
169)) / 30);
// Condition to check if number N
// is a heptadecagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void Main(string[] args)
{
// Given Number
int N = 17;
// Function call
if (isheptadecagonal(N))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by rutvik_56
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)