```
public class Solution {
private static final double epsilon = 10e-15;
public boolean isPowerOfThree(int n) {
double num = Math.log(n) / Math.log(3);
return Math.abs((num - Math.round(num) )) <= epsilon;
}
}
```

Why this works?

Each number which is power of 3 can be written as

*n = 3^k*

if we multiply number with log we get

*log(n) = log(3^k)*

from that k goes out of the log

*log(n) = k * log(3)*

*k = log(n) / log(3)*

k by the definition has to be positive whole number, so we check if this is true for log(n) / log(3) by subtracting this number from nearest whole number and we compare the result with some very small number (it is the way comparison looks like for double numbers)