题目
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?
解题思路
循环整除
代码
func isPowerOfFour(num int) bool {
if 0 >= num {
return false
}
if 1 == num {
return true
}
for ;num > 4 && num % 4 == 0; {
num = num / 4
}
if num % 4 == 0 {
return true
}
return false
}