Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example:
Given num = 16, return true.
Given num = 5, return false.


Follow up: Could you solve it without loops/recursion?

Credits:Special thanks to @yukuairoy  for adding this problem and creating all test cases.

Source: leetcode | Difficulty: EASY
Tags: Bit manipulation

Mathematics

Complete search

Starter code: class Solution:
    def isPowerOfFour(self, num: int) -> bool:
        

Solutions:
class Solution:

	def isPowerOfFour(self, num):
		return num != 0 and num & num - 1 == 0 and (num & 1431655765 == num)


class Solution:

	def isPowerOfFour(self, num):
		if num == 0:
			return False
		return not num & num - 1 and len(bin(num)) % 2 == 1


class Solution:

	def isPowerOfFour(self, num):
		if num < 1:
			return False
		return num - 1 & num == 0 and num & 1431655765 != 0


class Solution:

	def isPowerOfFour(self, num):
		if num <= 0:
			return False
		import math
		i = math.log10(num) / math.log10(4)
		return i.is_integer()


class Solution:

	def isPowerOfFour(self, num):
		while num <= 0:
			return False
		while num % 4 == 0:
			num //= 4
		return num == 1


class Solution:

	def isPowerOfFour(self, num):
		return bin(num).count('1') == 1 and bin(num).count('0') % 2 == 1 and (num > 0)


class Solution:

	def isPowerOfFour(self, num):
		if num == 1:
			return True
		target = 1
		while target < num:
			target *= 4
			if target == num:
				return True
		return False


class Solution:

	def isPowerOfFour(self, num):
		if num <= 0:
			return False
		sqrt_root = math.sqrt(num)
		if math.floor(sqrt_root) == sqrt_root:
			return int(sqrt_root) & int(sqrt_root - 1) == 0
		return False