📜  检查数字是否为8的幂

📅  最后修改于: 2021-04-23 19:57:42             🧑  作者: Mango

给定一个数字,检查它是否为8的幂。

例子 :

Input : n = 64
Output : Yes

Input : 75
Output : No

第一个解决方案

我们计算数字的log8(n)(如果它是整数),则n为8的幂。我们使用trunc(n)函数为双精度值找到最接近的整数。

C++
// C++ program to check if a number is power of 8
#include 
#include 
using namespace std;
 
/* function to check if power of 8 */
bool checkPowerof8(int n)
{
    /* calculate log8(n) */
    double i = log(n) / log(8);
 
    /* check if i is an integer or not */
    return (i - trunc(i) < 0.000001);
}
 
/* driver function */
int main()
{
    int n = 65;
    checkPowerof8(n) ? cout << "Yes" : cout << "No";
    return 0;
}


Java
// Java program to check if
// a number is power of 8
 
class GFG {
 
    // function to check
    // if power of 8
    static boolean checkPowerof8(int n)
    {
        /* calculate log8(n) */
        double i = Math.log(n) / Math.log(8);
 
        /* check if i is an integer or not */
        return (i - Math.floor(i) < 0.000001);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int n = 65;
        if (checkPowerof8(n))
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}
 
// This code is contributed by Sam007


Python3
# Python3 program to check
# if a number is power of 8
from math import log,trunc
 
# function to check if power of 8
def checkPowerof8(n):
 
    # calculate log8(n)
    i = log(n, 8)
 
    # check if i is an integer or not
    return (i - trunc(i) < 0.000001);
 
# Driver Code
n = 65
if checkPowerof8(n):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Mohit Kumar


C#
// C#  program to check if
// a number is power of 8
using System;
 
class GFG {
 
    // function to check
    // if power of 8
    static bool checkPowerof8(int n)
    {
 
        // calculate log8(n) */
        double i = Math.Log(n) / Math.Log(8);
 
        // check if i is an integer or not */
        return (i - Math.Floor(i) < 0.000001);
    }
 
    // Driver Code
    static public void Main()
    {
        int n = 65;
 
        if (checkPowerof8(n))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by akt_mit


PHP


C++
// C++ program to check if a number is power of 8
// using bit mask.
#include 
using namespace std;
 
/*function to check if power of 8*/
bool checkPowerof8(int n)
{
    return (n && !(n & (n - 1)) && !(n & 0xB6DB6DB6));
}
 
/*driver function*/
int main()
{
    int n = 65;
    checkPowerof8(n) ? cout << "Yes" : cout << "No";
 
    return 0;
}


Java
// Java program to check if a
// number is power of 8 using
// bit mask.
import java.util.*;
class GFG{
 
// function to check if
// power of 8
static boolean checkPowerof8(int n)
{
  return (n > 0 && (n & (n - 1)) > 0 &&
         (n & 0xB6DB6DB6) > 0);
}
 
// Driver code
public static void main(String[] args)
{
  int n = 65;
  if (checkPowerof8(n) == true)
    System.out.print("Yes" );
  else
    System.out.print("No");
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Python3 program to check if a number
# is power of 8
 
# function to check if power of 8
def checkPowerof8(n):
 
    return (n and not (n & (n - 1)) and
                  not (n & 0xB6DB6DB6))
 
# Driver Code
if __name__ == "__main__":
 
    n = 65
     
if checkPowerof8(n):
    print ("Yes") 
else:
    print ("No")
 
# This code is contributed by ita_c


C#
// C# program to check if a
// number is power of 8 using
// bit mask.
using System;
 
class GFG{
     
// Function to check if
// power of 8
static bool checkPowerof8(int n)
{
    return (n > 0 && (n & (n - 1)) > 0 &&
           (n & 0xB6DB6DB6) > 0);
}
 
// Driver code
static public void Main()
{
    int n = 65;
     
    if (checkPowerof8(n) == true)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by avanitrachhadiya2155


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to check if n is power of 8
bool checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    unsigned long long l = 1;
    while (i <= 63) {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
int main()
{
    int n = 65;
    if (checkPowerof8(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to check if n is power of 8
static boolean checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    long l = 1;
    while (i <= 63)
    {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
public static void main (String[] args)
{
     
    int n = 65;
    if (checkPowerof8(n))
        System.out.println ("Yes");
    else
        System.out.println( "No");
}
}
 
// This code is contributed by Tushil.


Python3
# Python3 implementation of the approach
 
# Function to check if n is power of 8
def checkPowerof8(n):
     
    # Variable i will denote the bit
    # that we are currently at
    i = 0
    l = 1
     
    while (i <= 63):
        l <<= i
 
        # If only set bit in n
        # is at position i
        if (l == n):
            return True
 
        # Get to next valid bit position
        i += 3
        l = 1
   
    return False
   
# Driver code
if __name__ == '__main__':
      
    n = 65
    if (checkPowerof8(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by math_lover


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to check if n is power of 8
static bool checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    long l = 1;
    while (i <= 63)
    {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
static public void Main ()
{
    int n = 65;
    if (checkPowerof8(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine( "No");
}
}
 
// This code is contributed by ajit.


Javascript


输出 :

No

第二解决方案
如果满足以下条件,则数字是8的幂。

  1. 该数字是2的幂。如果数字只有一个置1的位,即为n且n-1为0,则该数字为2的幂。
  2. 该数字仅在位置0或3或6或…上设置了一位。 30 [对于32位数字]。要检查其设置位的位置,我们可以使用掩码(0xB6DB6DB6) 16 =(10110110110110110110110110110110110) 2

以下是上述想法的实现。

C++

// C++ program to check if a number is power of 8
// using bit mask.
#include 
using namespace std;
 
/*function to check if power of 8*/
bool checkPowerof8(int n)
{
    return (n && !(n & (n - 1)) && !(n & 0xB6DB6DB6));
}
 
/*driver function*/
int main()
{
    int n = 65;
    checkPowerof8(n) ? cout << "Yes" : cout << "No";
 
    return 0;
}

Java

// Java program to check if a
// number is power of 8 using
// bit mask.
import java.util.*;
class GFG{
 
// function to check if
// power of 8
static boolean checkPowerof8(int n)
{
  return (n > 0 && (n & (n - 1)) > 0 &&
         (n & 0xB6DB6DB6) > 0);
}
 
// Driver code
public static void main(String[] args)
{
  int n = 65;
  if (checkPowerof8(n) == true)
    System.out.print("Yes" );
  else
    System.out.print("No");
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python3 program to check if a number
# is power of 8
 
# function to check if power of 8
def checkPowerof8(n):
 
    return (n and not (n & (n - 1)) and
                  not (n & 0xB6DB6DB6))
 
# Driver Code
if __name__ == "__main__":
 
    n = 65
     
if checkPowerof8(n):
    print ("Yes") 
else:
    print ("No")
 
# This code is contributed by ita_c

C#

// C# program to check if a
// number is power of 8 using
// bit mask.
using System;
 
class GFG{
     
// Function to check if
// power of 8
static bool checkPowerof8(int n)
{
    return (n > 0 && (n & (n - 1)) > 0 &&
           (n & 0xB6DB6DB6) > 0);
}
 
// Driver code
static public void Main()
{
    int n = 65;
     
    if (checkPowerof8(n) == true)
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by avanitrachhadiya2155

的PHP


Java脚本


输出 :

No

一个简单的观察结果就是,如果一个数字是8的幂,那么它只有一个1位的集合,并且该位位于位置1、4、7、10,…上。
因此,我们可以仅检查数字中设置的唯一位是否在这些位置之一,那么它是否是8的幂。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to check if n is power of 8
bool checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    unsigned long long l = 1;
    while (i <= 63) {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
int main()
{
    int n = 65;
    if (checkPowerof8(n))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}

Java

// Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function to check if n is power of 8
static boolean checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    long l = 1;
    while (i <= 63)
    {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
public static void main (String[] args)
{
     
    int n = 65;
    if (checkPowerof8(n))
        System.out.println ("Yes");
    else
        System.out.println( "No");
}
}
 
// This code is contributed by Tushil.

Python3

# Python3 implementation of the approach
 
# Function to check if n is power of 8
def checkPowerof8(n):
     
    # Variable i will denote the bit
    # that we are currently at
    i = 0
    l = 1
     
    while (i <= 63):
        l <<= i
 
        # If only set bit in n
        # is at position i
        if (l == n):
            return True
 
        # Get to next valid bit position
        i += 3
        l = 1
   
    return False
   
# Driver code
if __name__ == '__main__':
      
    n = 65
    if (checkPowerof8(n)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by math_lover

C#

// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to check if n is power of 8
static bool checkPowerof8(int n)
{
    // Variable i will denote the bit
    // that we are currently at
    int i = 0;
    long l = 1;
    while (i <= 63)
    {
        l <<= i;
 
        // If only set bit in n
        // is at position i
        if (l == n)
            return true;
 
        // Get to next valid bit position
        i += 3;
        l = 1;
    }
    return false;
}
 
// Driver code
static public void Main ()
{
    int n = 65;
    if (checkPowerof8(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine( "No");
}
}
 
// This code is contributed by ajit.

Java脚本


输出 :

No