给定一个整数N ,任务是检查它是否是一个中心的七边形数字。
Centered heptagonal number is centered figure number that represents a heptagon with dot in center and all other dot surrounding in heptagonal form..The first few Centered heptagonal number are 1, 8, 22, 43, 71, 106, 148, …
例子:
Input: N = 8
Output: Yes
Explanation:
8 is the Second Centered heptagonal number.
Input: 20
Output: No
Explanation:
20 is not a Centered heptagonal number.
方法:
为了解决上述问题,我们必须知道中心七边形数的K个项为:
因为我们必须检查给定的数字是否可以表示为中心的七边形数字。可以通过将方程式概括为以下内容进行检查:
=>
=>
最后,使用此公式检查计算值是否为整数,如果为整数,则表示N为中心七边形数。
下面是上述方法的实现:
C++
// C++ implementation to check that
// a number is a Centered
// heptagonal number or not
#include
using namespace std;
// Function to check that the
// number is a Centered
// heptagonal number
bool isCenteredheptagonal(int N)
{
float n = (7 + sqrt(56 * N - 7)) / 14;
// Condition to check if the
// number is a Centered heptagonal number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
int n = 8;
// Function call
if (isCenteredheptagonal(n)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java implementation to check that
// a number is a Centered
// heptagonal number or not
import java.lang.Math;
class GFG
{
// Function to check that the
// number is a Centered
// heptagonal number
public static boolean isCenteredheptagonal(int N)
{
double n = (7 + Math.sqrt(56 * N - 7)) / 14;
// Condition to check if the
// number is a Centered heptagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
int n = 8;
// Function call
if (isCenteredheptagonal(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
# heptagonal number or not
import math
# Function to check that the
# number is a centered
# heptagonal number
def isCenteredheptagonal(N):
n = (7 + math.sqrt(56 * N - 7)) / 14
# Condition to check if the number
# is a centered heptagonal number
return (n - int(n)) == 0
# Driver Code
n = 8
# Function call
if (isCenteredheptagonal(n)):
print("Yes")
else:
print("No")
# This code is contributed by ShubhamCoder
C#
// C# implementation to check that
// a number is a centered
// heptagonal number or not
using System;
class GFG{
// Function to check that the
// number is a centered
// heptagonal number
static bool isCenteredheptagonal(int N)
{
double n = (7 + Math.Sqrt(56 * N - 7)) / 14;
// Condition to check if the number
// is a centered heptagonal number
return (n - (int)n) == 0;
}
// Driver Code
static public void Main ()
{
int n = 8;
// Function call
if (isCenteredheptagonal(n))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by ShubhamCoder
Javascript
输出:
Yes