public boolean isPowerOfThree(int n) {
return (Math.log10(n) / Math.log10(3)) % 1 == 0;
}
This solution uses math knowledge. If log(n) / log(3) returns an int, then n is a power of 3.
To be precise, (Math.log10(n) / Math.log10(3))
is a double, not an int. The reason why log10
works but not log
(natural log) is only a coincidence in my opinion. You can check out my post here.
I don't think it's a coincidence. (Math.log10(n) / Math.log10(3)) is indeed a double. But when n is a power of 3, (Math.log10(n) / Math.log10(3)) % 1 will be equal to 0. Although it's still a double , but it's a int in double format. That's why this solution works. I actually first tried log(n) and I noticed that log(243) gave me 4.99999... That's why I used log10() instead. But I think that's Java's problem, not this method's problem. Because ideally log(243) should give me 5.0