给定数字N ,任务是检查N是否为中心十六进制数。如果数字N是中心十六进制数字,则打印“是”,否则打印“否” 。
Centered Hexadecagonal Number represents a dot in the centre and other dots around it in successive Hexadecagonal(16 sided polygon) layers… The first few Centered Hexadecagonal Numbers are 1, 17, 49, 97, 161, 241 …
例子:
Input: N = 17
Output: Yes
Explanation:
Second Centered hexadecagonal number is 17.
Input: N = 20
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 Centered hexadecagonal number
bool isCenteredhexadecagonal(int N)
{
float n
= (8 + sqrt(32 * N + 32))
/ 16;
// Condition to check if the N is a
// Centered hexadecagonal number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 17;
// Function call
if (isCenteredhexadecagonal(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG {
// Function to check if the number N
// is a centered hexadecagonal number
static boolean isCenteredhexadecagonal(int N)
{
double n = (8 + Math.sqrt(32 * N + 32)) / 16;
// Condition to check if the N is a
// centered hexadecagonal number
return (n - (int)n) == 0;
}
// Driver code
public static void main(String[] args)
{
// Given Number
int N = 17;
// Function call
if (isCenteredhexadecagonal(N))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by coder001
Python3
# Python3 program for the above approach
import numpy as np
# Function to check if the number N
# is a Centered hexadecagonal number
def isCenteredhexadecagonal(N):
n = (8 + np.sqrt(32 * N + 32)) / 16
# Condition to check if the N is a
# Centered hexadecagonal number
return (n - int(n)) == 0
# Driver Code
N = 17
# Function call
if (isCenteredhexadecagonal(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 centered hexadecagonal number
static bool isCenteredhexadecagonal(int N)
{
double n = (8 + Math.Sqrt(32 * N + 32)) / 16;
// Condition to check if the N is a
// centered hexadecagonal number
return (n - (int)n) == 0;
}
// Driver code
public static void Main(string[] args)
{
// Given Number
int N = 17;
// Function call
if (isCenteredhexadecagonal(N))
{
Console.Write("Yes");
}
else
{
Console.Write("No");
}
}
}
// This code is contributed by rutvik_56
Javascript
输出:
Yes
时间复杂度: O(1)
辅助空间: O(1)