class Solution {
public:
bool isPowerOfThree(int n) {
return fmod(log10(n)/log10(3), 1)==0;
}
};
1 line C++ no recursion/loop

@butterwang0
I'm also confused  shouldn'tfmod(log10(n), log10(3)) == 0
iffmod(log10(n)/log10(3), 1) == 0
?

@SuperCC
log10(6) = 0.77815125
log10(3) = 0.477121255
0.77815125 % 0.477121255 = 0.301029995Given above, shouldn't
fmod(log10(6), log10(3))
= 0.301029995 instead of 0? Can you explain?


Here is why log10() was chosen,
cout << (fmod(log(81)/log(3), 1)==0); // 1 cout << (fmod(log10(81)/log10(3), 1)==0); // 1 cout << (fmod(log(243)/log(3), 1)==0); // 0 cout << (fmod(log10(243)/log10(3), 1)==0); // 1 cout << (fmod(log(729)/log(3), 1)==0); // 1 cout << (fmod(log10(729)/log10(3), 1)==0); // 1
You'll be happily kicked out if you compare double with 0 in interview.
===
Just found the precision bug is also explained in this thread