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?
class Solution {
public:
bool isPowerOfFour(int num) {
if(num <= 0)
return false;
return !fmod(log10(num)/log10(4), 1);
}
};
类似与该题,求解某个数num是power of k,则num = k ^ n,因此log10(num) = n * log10(k),再利用fmod函数判断log10(num) / log10(k) 是不是整数,