Problem
Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n = = 4 x n == 4^x n==4x.
Algorithm
Use the bit operations.
Code
python3
class Solution:
def isPowerOfFour(self, n: int) -> bool:
'''
if n <= 0:
return False
m = int(log(n)/log(4))
return 4 ** m == n
'''
if n <= 0:
return False
while n > 1:
if n & 3 != 0:
return False
n >>= 2
return True