📜  程序检查N是否是八进制数字

📅  最后修改于: 2021-04-22 09:44:00             🧑  作者: Mango

给定数字N ,任务是检查N是否为八进制数。如果数字N是八进制数字,则打印“是”,否则打印“否”

例子:

方法:

  1. 十八进制数的第K项为
    K^{th} Term = \frac{16*K^{2} - 14*K}{2}
  2. 因为我们必须检查给定的数字是否可以表示为八进制数。可以检查为:
  1. 如果使用上述公式计算出的K的值为整数,则N为八进制数。
  2. 其他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