Q:
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?
solution:
解法很多,思路和之前的方法一样:
(LeetCode 326)Power of Three
4x=n
x=log(n)/log(4)
判断x是否为整数
class Solution {
public:
bool isPowerOfFour(int num) {
double x = log10(num)/log10(4);
if(x==int(x))return true;
return false;
}
};