原题:
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
AC后的代码(注意,该代码只对素数有用):
class Solution {
public:
bool isPowerOfThree(int n) {
//some int =3^k;
//k=log3(some max int)
if(n<=0) return false;
const int maxint =0x7fffffff;
int k=log(maxint)/log(3);
int somgbig3=pow(3,k);
return (somgbig3%n==0);
}
};