给定整数N ,任务是检查它是否为二十六边形数字。
Icosihexagonal number is class of figurate number. It has 26 – sided polygon called Icosihexagon. The N-th Icosihexagonal number count’s the 26 number of dots and all other dots are surrounding with a common sharing corner and make a pattern. The first few Icosihexagonol numbers are 1, 26, 75, 148 …
例子:
Input: N = 26
Output: Yes
Explanation:
Second icosihexagonal number is 26.
Input: 30
Output: No
方法:
- 二十进制六边形数的第K个项为
- 因为我们必须检查给定的数字是否可以表示为二十面体六边形。可以检查如下–
=>
=>
- 最后,检查使用此公式计算出的值是否为整数,这意味着N为二十六边形数字。
下面是上述方法的实现:
C++
// C++ program to check whether
// a number is a icosihexagonal number
// or not
#include
using namespace std;
// Function to check whether the
// number is a icosihexagonal number
bool isicosihexagonal(int N)
{
float n
= (22 + sqrt(192 * N + 484))
/ 48;
// Condition to check if the
// number is a icosihexagonal number
return (n - (int)n) == 0;
}
// Driver code
int main()
{
int i = 26;
if (isicosihexagonal(i)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program to check whether the
// number is a icosihexagonal number
// or not
class GFG{
// Function to check whether the
// number is a icosihexagonal number
static boolean isicosihexagonal(int N)
{
float n = (float) ((22 + Math.sqrt(192 * N +
484)) / 48);
// Condition to check if the number
// is a icosihexagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
// Given number
int N = 26;
// Function call
if (isicosihexagonal(N))
{
System.out.print("Yes");
}
else
{
System.out.print("No");
}
}
}
// This code is contributed by shubham
Python3
# Python3 program to check whether
# a number is a icosihexagonal number
# or not
import numpy as np
# Function to check whether the
# number is a icosihexagonal number
def isicosihexagonal(N):
n = (22 + np.sqrt(192 * N + 484)) / 48
# Condition to check if the
# number is a icosihexagonal number
return (n - (int(n))) == 0
# Driver code
i = 26
if (isicosihexagonal(i)):
print ("Yes")
else:
print ("No")
# This code is contributed by PratikBasu
C#
// C# program to check whether the
// number is a icosihexagonal number
// or not
using System;
class GFG{
// Function to check whether the
// number is a icosihexagonal number
static bool isicosihexagonal(int N)
{
float n = (float)((22 + Math.Sqrt(192 * N +
484)) / 48);
// Condition to check if the number
// is a icosihexagonal number
return (n - (int)n) == 0;
}
// Driver Code
public static void Main()
{
// Given number
int N = 26;
// Function call
if (isicosihexagonal(N))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by Code_Mech
Javascript
输出:
Yes