📌  相关文章
📜  查找给定数字是否为4的幂

📅  最后修改于: 2021-05-06 21:12:09             🧑  作者: Mango

给定整数n,请确定它是否为4的幂。
例子 :

Input : 16
Output : 16 is a power of 4

Input : 20
Output : 20 is not a power of 4

我们强烈建议您单击此处并进行实践,然后再继续解决方案。

1.一种简单的方法是以4为底数取给定数字的对数,如果我们得到一个整数,则该数字为4的幂。
2.另一个解决方案是将数字除以4,即迭代地做n = n / 4。在任何迭代中,如果n%4变为非零且n不为1,则n不是4的幂。否则,n是4的幂。

C++
// C++ program to find whether a given
// number is a power of 4 or not
#include
 
using namespace std;
#define bool int
 
class GFG
{
     
/* Function to check if x is power of 4*/
public : bool isPowerOfFour(int n)
{
    if(n == 0)
        return 0;
    while(n != 1)
    {
        if(n % 4 != 0)
            return 0;
        n = n / 4;
    }
    return 1;
}
};
 
/*Driver code*/
int main()
{
    GFG g;
    int test_no = 64;
    if(g.isPowerOfFour(test_no))
        cout << test_no << " is a power of 4";
    else
        cout << test_no << "is not a power of 4";
    getchar();
}
 
// This code is contributed by SoM15242


C
#include
#define bool int
 
/* Function to check if x is power of 4*/
bool isPowerOfFour(int n)
{
  if(n == 0)
    return 0;
  while(n != 1)
  {   
   if(n % 4 != 0)
      return 0;
    n = n / 4;     
  }
  return 1;
}
 
/*Driver program to test above function*/
int main()
{
  int test_no = 64;
  if(isPowerOfFour(test_no))
    printf("%d is a power of 4", test_no);
  else
    printf("%d is not a power of 4", test_no);
  getchar();
}


Java
// Java code to check if given
// number is power of 4 or not
 
class GFG {
 
    // Function to check if
    // x is power of 4
    static int isPowerOfFour(int n)
    {
        if(n == 0)
        return 0;
        while(n != 1)
        {
            if(n % 4 != 0)
            return 0;
            n = n / 4;    
        }
        return 1;
    }
 
    // Driver program
    public static void main(String[] args)
    {
        int test_no = 64;
        if(isPowerOfFour(test_no) == 1)
         System.out.println(test_no +
                           " is a power of 4");
        else
         System.out.println(test_no +
                           "is not a power of 4");
    }
}
 
// This code is contributed
// by  prerna saini


Python3
# Python3 program to check if given
# number is power of 4 or not
 
# Function to check if x is power of 4
def isPowerOfFour(n):
    if (n == 0):
        return False
    while (n != 1):
            if (n % 4 != 0):
                return False
            n = n // 4
             
    return True
 
# Driver code
test_no = 64
if(isPowerOfFour(64)):
    print(test_no, 'is a power of 4')
else:
    print(test_no, 'is not a power of 4')
 
# This code is contributed by Danish Raza


C#
// C# code to check if given
// number is power of 4 or not
using System;
 
class GFG {
     
    // Function to check if
    // x is power of 4
    static int isPowerOfFour(int n)
    {
        if (n == 0)
            return 0;
        while (n != 1) {
            if (n % 4 != 0)
                return 0;
            n = n / 4;
        }
        return 1;
    }
 
    // Driver code
    public static void Main()
    {
        int test_no = 64;
        if (isPowerOfFour(test_no) == 1)
            Console.Write(test_no +
                    " is a power of 4");
        else
            Console.Write(test_no +
                " is not a power of 4");
    }
}
 
// This code is contributed by Sam007


PHP


Javascript


C++
// C++ program to check
// if given number is
// power of 4 or not
#include
 
using namespace std;
 
bool isPowerOfFour(unsigned int n)
{
    int count = 0;
 
    /*Check if there is only one bit set in n*/
    if ( n && !(n&(n-1)) )
    {
        /* count 0 bits before set bit */
        while(n > 1)
        {
            n >>= 1;
            count += 1;
        }
 
        /*If count is even then
        return true else false*/
        return (count%2 == 0)? 1 :0;
    }
 
    /* If there are more than 1 bit set
    then n is not a power of 4*/
    return 0;
}
 
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
}
 
// This code is contributed by Shivi_Aggarwal


C
#include
#define bool int
 
bool isPowerOfFour(unsigned int n)
{
  int count = 0;
 
  /*Check if there is only one bit set in n*/
  if ( n && !(n&(n-1)) )
  {
     /* count 0 bits before set bit */
     while(n > 1)
     {
       n  >>= 1;
       count += 1;
     }     
 
    /*If count is even then return true else false*/
    return (count%2 == 0)? 1 :0;
  }
 
  /* If there are more than 1 bit set
    then n is not a power of 4*/
  return 0;
}   
 
/*Driver program to test above function*/
int main()
{
   int test_no = 64;
   if(isPowerOfFour(test_no))
     printf("%d is a power of 4", test_no);
   else
     printf("%d is not a power of 4", test_no);
   getchar();
}


Java
// Java program to check
// if given number is
// power of 4 or not
import java.io.*;
class GFG
{
    static int isPowerOfFour(int n)
    {
        int count = 0;
         
        /*Check if there is
        only one bit set in n*/
        int x = n & (n - 1);
         
        if ( n > 0 && x == 0)
        {
            /* count 0 bits
            before set bit */
            while(n > 1)
            {
                n >>= 1;
                count += 1;
            }
         
            /*If count is even
            then return true
            else false*/
            return (count % 2 == 0) ? 1 : 0;
        }
         
            /* If there are more than
            1 bit set then n is not a
            power of 4*/
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no)>0)
            System.out.println(test_no +
                               " is a power of 4");
        else
            System.out.println(test_no +
                               " is not a power of 4");
    }
}
 
// This code is contributed by mits


Python3
# Python3 program to check if given
# number is power of 4 or not
 
# Function to check if x is power of 4
def isPowerOfFour(n):
     
    count = 0
     
    # Check if there is only one
    # bit set in n
    if (n and (not(n & (n - 1)))):
         
        # count 0 bits before set bit
        while(n > 1):
            n >>= 1
            count += 1
         
        # If count is even then return
        # true else false
        if(count % 2 == 0):
            return True
        else:
            return False
 
# Driver code
test_no = 64
if(isPowerOfFour(64)):
    print(test_no, 'is a power of 4')
else:
    print(test_no, 'is not a power of 4')
 
# This code is contribued by Danish Raza


C#
// C# program to check if given
// number is power of 4 or not
using System;
 
class GFG {
     
    static int isPowerOfFour(int n)
    {
        int count = 0;
         
        /*Check if there is only one bit
        set in n*/
        int x = n & (n-1);
         
        if ( n > 0 && x == 0)
        {
            /* count 0 bits before set bit */
            while(n > 1)
            {
                n >>= 1;
                count += 1;
            }
         
            /*If count is even then return
            true else false*/
            return (count % 2 == 0) ? 1 : 0;
        }
         
            /* If there are more than 1 bit set
            then n is not a power of 4*/
        return 0;
    }
 
    /*Driver program to test above function*/
    static void Main()
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no)>0)
            Console.WriteLine("{0} is a power of 4",
                                          test_no);
        else
            Console.WriteLine("{0} is not a power of 4",
                                             test_no);
    }
}
 
// This Code is Contributed by mits


PHP
 1)
    {
    $n >>= 1;
    $count += 1;
    }    
 
    /*If count is even then return true else false*/
    return ($count%2 == 0)? 1 :0;
}
 
/* If there are more than 1 bit set
    then n is not a power of 4*/
return 0;
}
 
/*Driver program to test above function*/
 
    $test_no = 64;
     
if(isPowerOfFour($test_no))
 
    echo $test_no, " is a power of 4";
 
else
 
    echo $test_no, " not is a power of 4";
     
 
#This Code is Contributed by Ajit
?>


Javascript


C++
// C++ program to check
// if given number is
// power of 4 or not
#include
 
using namespace std;
 
bool isPowerOfFour(unsigned int n)
{
    return n !=0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}
 
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
}


C
// C program to check
// if given number is
// power of 4 or not
#include
#define bool int
 
bool isPowerOfFour(unsigned int n)
{
  return n != 0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}   
 
/*Driver program to test above function*/
int main() {
   int test_no = 64;
   if(isPowerOfFour(test_no))
     printf("%d is a power of 4", test_no);
   else
     printf("%d is not a power of 4", test_no);
   getchar();
}


Java
// Java program to check
// if given number is
// power of 4 or not
import java.io.*;
class GFG {
    static boolean isPowerOfFour(int n) {
        return n != 0 && ((n&(n-1)) == 0) && (n & 0xAAAAAAAA) == 0;
    }
 
    // Driver Code
    public static void main(String[] args) {
        int test_no = 64;
         
        if(isPowerOfFour(test_no))
            System.out.println(test_no +
                               " is a power of 4");
        else
            System.out.println(test_no +
                               " is not a power of 4");
    }
}


Python3
# Python3 program to check
# if given number is
# power of 4 or not
def isPowerOfFour(n):
    return (n != 0 and
          ((n & (n - 1)) == 0) and
            not(n & 0xAAAAAAAA));
 
# Driver code
test_no = 64;
if(isPowerOfFour(test_no)):
    print(test_no ,"is a power of 4");
else:
    print(test_no , "is not a power of 4");
 
# This code contributed by Rajput-Ji


C#
// C# program to check
// if given number is
// power of 4 or not
using System;
 
class GFG
{
    static bool isPowerOfFour(int n)
    {
        return n != 0 && ((n&(n-1)) == 0) &&
                        (n & 0xAAAAAAAA) == 0;
    }
 
    // Driver Code
    static void Main()
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no))
            Console.WriteLine("{0} is a power of 4",
                                        test_no);
        else
            Console.WriteLine("{0} is not a power of 4",
                                            test_no);
    }
}
 
// This code is contributed by mohit kumar 29


C++
// C++ program to check 
// if given number is 
// power of 4 or not 
#include
   
using namespace std;
 
float logn(int n, int r)
{
    return log(n) / log(r);
}
 
bool isPowerOfFour(int n)
{
    //0 is not considered as a power
    //of 4
    if(n == 0)
        return false;
    return floor(logn(n,4))==ceil(logn(n,4));
} 
   
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
    return 0;
}


Java
// Java program to check
// if given number is
// power of 4 or not
import java.util.*;
class GFG{
 
static double logn(int n,
                   int r)
{
  return Math.log(n) /
         Math.log(r);
}
 
static boolean isPowerOfFour(int n)
{
  // 0 is not considered
  // as a power of 4
  if (n == 0)
    return false;
  return Math.floor(logn(n, 4)) ==
         Math.ceil(logn(n, 4));
}
 
// Driver code
public static void main(String[] args)
{
  int test_no = 64;
  if (isPowerOfFour(test_no))
    System.out.print(test_no +
                     " is a power of 4");
  else
    System.out.print(test_no +
                     " is not a power of 4");
}
}
 
// This code is contributed by Amit Katiyar


Python3
# Python3 program to check
# if given number is
# power of 4 or not
import math
 
def logn(n, r):
    return math.log(n) / math.log(r)
 
def isPowerOfFour(n):
     
    # 0 is not considered
    # as a power of 4
    if (n == 0):
        return False
         
    return (math.floor(logn(n, 4)) ==
             math.ceil(logn(n, 4)))
 
# Driver code
if __name__ == '__main__':
     
    test_no = 64
     
    if (isPowerOfFour(test_no)):
        print(test_no, " is a power of 4")
    else:
        print(test_no, " is not a power of 4")
 
# This code is contributed by Amit Katiyar


C#
// C# program to check
// if given number is
// power of 4 or not
using System;
class GFG{
 
static double logn(int n,
                   int r)
{
  return Math.Log(n) /
         Math.Log(r);
}
 
static bool isPowerOfFour(int n)
{
  // 0 is not considered
  // as a power of 4
  if (n == 0)
    return false;
  return Math.Floor(logn(n, 4)) ==
         Math.Ceiling(logn(n, 4));
}
 
// Driver code
public static void Main(String[] args)
{
  int test_no = 64;
  if (isPowerOfFour(test_no))
    Console.Write(test_no +
                  " is a power of 4");
  else
    Console.Write(test_no +
                  " is not a power of 4");
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出 :

64 is a power of 4

时间复杂度: O(log 4 n)

辅助空间: O(1)
3.如果满足以下条件,则数字n是4的幂。
a)在n的二进制表示中仅设置一位(或n为2的幂)
b)在(仅)置位之前的零位计数为偶数。
例如,16(10000)是4的幂,因为仅设置了一位,而在设置的位4为偶数之前计数为0。
感谢Geek4u提出了建议的方法并提供了代码。

C++

// C++ program to check
// if given number is
// power of 4 or not
#include
 
using namespace std;
 
bool isPowerOfFour(unsigned int n)
{
    int count = 0;
 
    /*Check if there is only one bit set in n*/
    if ( n && !(n&(n-1)) )
    {
        /* count 0 bits before set bit */
        while(n > 1)
        {
            n >>= 1;
            count += 1;
        }
 
        /*If count is even then
        return true else false*/
        return (count%2 == 0)? 1 :0;
    }
 
    /* If there are more than 1 bit set
    then n is not a power of 4*/
    return 0;
}
 
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
}
 
// This code is contributed by Shivi_Aggarwal

C

#include
#define bool int
 
bool isPowerOfFour(unsigned int n)
{
  int count = 0;
 
  /*Check if there is only one bit set in n*/
  if ( n && !(n&(n-1)) )
  {
     /* count 0 bits before set bit */
     while(n > 1)
     {
       n  >>= 1;
       count += 1;
     }     
 
    /*If count is even then return true else false*/
    return (count%2 == 0)? 1 :0;
  }
 
  /* If there are more than 1 bit set
    then n is not a power of 4*/
  return 0;
}   
 
/*Driver program to test above function*/
int main()
{
   int test_no = 64;
   if(isPowerOfFour(test_no))
     printf("%d is a power of 4", test_no);
   else
     printf("%d is not a power of 4", test_no);
   getchar();
}

Java

// Java program to check
// if given number is
// power of 4 or not
import java.io.*;
class GFG
{
    static int isPowerOfFour(int n)
    {
        int count = 0;
         
        /*Check if there is
        only one bit set in n*/
        int x = n & (n - 1);
         
        if ( n > 0 && x == 0)
        {
            /* count 0 bits
            before set bit */
            while(n > 1)
            {
                n >>= 1;
                count += 1;
            }
         
            /*If count is even
            then return true
            else false*/
            return (count % 2 == 0) ? 1 : 0;
        }
         
            /* If there are more than
            1 bit set then n is not a
            power of 4*/
        return 0;
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no)>0)
            System.out.println(test_no +
                               " is a power of 4");
        else
            System.out.println(test_no +
                               " is not a power of 4");
    }
}
 
// This code is contributed by mits

Python3

# Python3 program to check if given
# number is power of 4 or not
 
# Function to check if x is power of 4
def isPowerOfFour(n):
     
    count = 0
     
    # Check if there is only one
    # bit set in n
    if (n and (not(n & (n - 1)))):
         
        # count 0 bits before set bit
        while(n > 1):
            n >>= 1
            count += 1
         
        # If count is even then return
        # true else false
        if(count % 2 == 0):
            return True
        else:
            return False
 
# Driver code
test_no = 64
if(isPowerOfFour(64)):
    print(test_no, 'is a power of 4')
else:
    print(test_no, 'is not a power of 4')
 
# This code is contribued by Danish Raza

C#

// C# program to check if given
// number is power of 4 or not
using System;
 
class GFG {
     
    static int isPowerOfFour(int n)
    {
        int count = 0;
         
        /*Check if there is only one bit
        set in n*/
        int x = n & (n-1);
         
        if ( n > 0 && x == 0)
        {
            /* count 0 bits before set bit */
            while(n > 1)
            {
                n >>= 1;
                count += 1;
            }
         
            /*If count is even then return
            true else false*/
            return (count % 2 == 0) ? 1 : 0;
        }
         
            /* If there are more than 1 bit set
            then n is not a power of 4*/
        return 0;
    }
 
    /*Driver program to test above function*/
    static void Main()
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no)>0)
            Console.WriteLine("{0} is a power of 4",
                                          test_no);
        else
            Console.WriteLine("{0} is not a power of 4",
                                             test_no);
    }
}
 
// This Code is Contributed by mits

的PHP

 1)
    {
    $n >>= 1;
    $count += 1;
    }    
 
    /*If count is even then return true else false*/
    return ($count%2 == 0)? 1 :0;
}
 
/* If there are more than 1 bit set
    then n is not a power of 4*/
return 0;
}
 
/*Driver program to test above function*/
 
    $test_no = 64;
     
if(isPowerOfFour($test_no))
 
    echo $test_no, " is a power of 4";
 
else
 
    echo $test_no, " not is a power of 4";
     
 
#This Code is Contributed by Ajit
?>

Java脚本


输出:

64 is a power of 4

4.如果满足以下条件,则数字n是4的幂。
a)在n的二进制表示中仅设置一位(或n为2的幂)
b)这些位不与(&)模式0xAAAAAAAA的任何部分
例如:16(10000)是4的幂,因为仅设置了一位,并且0x10&0xAAAAAAAA为零。
感谢Sarthak Sahu提出了该方法。

C++

// C++ program to check
// if given number is
// power of 4 or not
#include
 
using namespace std;
 
bool isPowerOfFour(unsigned int n)
{
    return n !=0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}
 
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
}

C

// C program to check
// if given number is
// power of 4 or not
#include
#define bool int
 
bool isPowerOfFour(unsigned int n)
{
  return n != 0 && ((n&(n-1)) == 0) && !(n & 0xAAAAAAAA);
}   
 
/*Driver program to test above function*/
int main() {
   int test_no = 64;
   if(isPowerOfFour(test_no))
     printf("%d is a power of 4", test_no);
   else
     printf("%d is not a power of 4", test_no);
   getchar();
}

Java

// Java program to check
// if given number is
// power of 4 or not
import java.io.*;
class GFG {
    static boolean isPowerOfFour(int n) {
        return n != 0 && ((n&(n-1)) == 0) && (n & 0xAAAAAAAA) == 0;
    }
 
    // Driver Code
    public static void main(String[] args) {
        int test_no = 64;
         
        if(isPowerOfFour(test_no))
            System.out.println(test_no +
                               " is a power of 4");
        else
            System.out.println(test_no +
                               " is not a power of 4");
    }
}

Python3

# Python3 program to check
# if given number is
# power of 4 or not
def isPowerOfFour(n):
    return (n != 0 and
          ((n & (n - 1)) == 0) and
            not(n & 0xAAAAAAAA));
 
# Driver code
test_no = 64;
if(isPowerOfFour(test_no)):
    print(test_no ,"is a power of 4");
else:
    print(test_no , "is not a power of 4");
 
# This code contributed by Rajput-Ji

C#

// C# program to check
// if given number is
// power of 4 or not
using System;
 
class GFG
{
    static bool isPowerOfFour(int n)
    {
        return n != 0 && ((n&(n-1)) == 0) &&
                        (n & 0xAAAAAAAA) == 0;
    }
 
    // Driver Code
    static void Main()
    {
        int test_no = 64;
         
        if(isPowerOfFour(test_no))
            Console.WriteLine("{0} is a power of 4",
                                        test_no);
        else
            Console.WriteLine("{0} is not a power of 4",
                                            test_no);
    }
}
 
// This code is contributed by mohit kumar 29

输出:

64 is a power of 4

时间复杂度: O(log 4 n)

辅助空间: O(1)
为什么选择0xAAAAAAAA?这是因为位表示具有2的幂而不是4的幂。例如2、8、32等。
5.如果floor(log4(num))= ceil(log4(num) ,则数字将是4的幂,因为以4的幂表示的log4始终是整数。

下面是上述方法的实现。

C++

// C++ program to check 
// if given number is 
// power of 4 or not 
#include
   
using namespace std;
 
float logn(int n, int r)
{
    return log(n) / log(r);
}
 
bool isPowerOfFour(int n)
{
    //0 is not considered as a power
    //of 4
    if(n == 0)
        return false;
    return floor(logn(n,4))==ceil(logn(n,4));
} 
   
/*Driver code*/
int main()
{
    int test_no = 64;
    if(isPowerOfFour(test_no))
        cout << test_no << " is a power of 4" ;
    else
        cout << test_no << " is not a power of 4";
    return 0;
}

Java

// Java program to check
// if given number is
// power of 4 or not
import java.util.*;
class GFG{
 
static double logn(int n,
                   int r)
{
  return Math.log(n) /
         Math.log(r);
}
 
static boolean isPowerOfFour(int n)
{
  // 0 is not considered
  // as a power of 4
  if (n == 0)
    return false;
  return Math.floor(logn(n, 4)) ==
         Math.ceil(logn(n, 4));
}
 
// Driver code
public static void main(String[] args)
{
  int test_no = 64;
  if (isPowerOfFour(test_no))
    System.out.print(test_no +
                     " is a power of 4");
  else
    System.out.print(test_no +
                     " is not a power of 4");
}
}
 
// This code is contributed by Amit Katiyar

Python3

# Python3 program to check
# if given number is
# power of 4 or not
import math
 
def logn(n, r):
    return math.log(n) / math.log(r)
 
def isPowerOfFour(n):
     
    # 0 is not considered
    # as a power of 4
    if (n == 0):
        return False
         
    return (math.floor(logn(n, 4)) ==
             math.ceil(logn(n, 4)))
 
# Driver code
if __name__ == '__main__':
     
    test_no = 64
     
    if (isPowerOfFour(test_no)):
        print(test_no, " is a power of 4")
    else:
        print(test_no, " is not a power of 4")
 
# This code is contributed by Amit Katiyar

C#

// C# program to check
// if given number is
// power of 4 or not
using System;
class GFG{
 
static double logn(int n,
                   int r)
{
  return Math.Log(n) /
         Math.Log(r);
}
 
static bool isPowerOfFour(int n)
{
  // 0 is not considered
  // as a power of 4
  if (n == 0)
    return false;
  return Math.Floor(logn(n, 4)) ==
         Math.Ceiling(logn(n, 4));
}
 
// Driver code
public static void Main(String[] args)
{
  int test_no = 64;
  if (isPowerOfFour(test_no))
    Console.Write(test_no +
                  " is a power of 4");
  else
    Console.Write(test_no +
                  " is not a power of 4");
}
}
 
// This code is contributed by 29AjayKumar

Java脚本


输出:

64 is a power of 4