Problem description:

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

Example 1:

1
2
Input: 16
Output: true

Example 2:

1
2
Input: 5
Output: false

Solution:

1
2
3
4
5
class Solution:
def isPowerOfFour(self, num: int) -> bool:
while num > 0 and num%4 == 0:
num/= 4
return num == 1

time complexity: $O(n)$
space complexity: $O(1)$
reference:
related problem: