public class Solution326_3 {
public boolean isPowerOfThree(int n) {
return n > 0 && 1162261467 % n == 0;
}
}
One line solution using mod

1162261467 is the largest power of 3 that can fit into an integer datatype.
1162261467 = 3^19 + 0The given
n
can be expressed as [(a power of 3) + (somex
)]. I think it is fairly elementary to be able to prove that ifx
is 0(which happens iff n is a power of 3), 1162261467 % n = 0.Beautiful solution :)