给定数字N ,任务是检查N是否为八进制数。如果数字N是八进制数字,则打印“是”,否则打印“否” 。
Octadecagon Number is a 18-sided polygon. The first few Octadecagon Numbers are 1, 18, 51, 100, 165, 246, 343, …
例子:
Input: N = 18
Output: Yes
Explanation:
Second Octadecagon number is 18.
Input: N = 30
Output: No
方法:
- 十八进制数的第K个项为
- 因为我们必须检查给定的数字是否可以表示为八进制数。可以检查为:
=>
=>
- 如果使用上述公式计算出的K的值为整数,则N为八进制数。
- 其他N不是十八进制数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N is a
// Octadecagon Number
bool isOctadecagon(int N)
{
float n
= (14 + sqrt(128 * N + 196))
/ 32;
// Condition to check if the
// number is a Octadecagon number
return (n - (int)n) == 0;
}
// Driver Code
int main()
{
// Given Number
int N = 18;
// Function call
if (isOctadecagon(N)) {
cout << "Yes";
}
else {
cout << "No";
}
return 0;
}
Java
// Java program for the above approach
import java.lang.Math;
class GFG{
// Function to check if N is a
// octadecagon Number
public static boolean isOctadecagon(int N)
{
double n = (14 + Math.sqrt(128 * N +
196)) / 32;
// Condition to check if the
// number is a octadecagon number
return (n - (int)n) == 0;
}
// Driver Code
public static void main(String[] args)
{
// Given Number
int N = 18;
// Function call
if (isOctadecagon(N))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by divyeshrabadiya07
Python3
# Python3 program for the above approach
import math
# Function to check if N is a
# octadecagon number
def isOctadecagon(N):
n = (14 + math.sqrt(128 * N + 196)) // 32
# Condition to check if the
# number is a octadecagon number
return ((n - int(n)) == 0)
# Driver code
if __name__=='__main__':
# Given number
N = 18
# Function Call
if isOctadecagon(N):
print('Yes')
else:
print('No')
# This code is contributed by rutvik_56
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if N is a
// octadecagon Number
public static bool isOctadecagon(int N)
{
double n = (14 + Math.Sqrt(128 * N +
196)) / 32;
// Condition to check if the
// number is a octadecagon number
return (n - (int)n) == 0;
}
// Driver Code
public static void Main(String[] args)
{
// Given Number
int N = 18;
// Function call
if (isOctadecagon(N))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Yes