```
public boolean isPowerOfThree(int n) {
if (n == 3 || n == 1) {
return true;
}
if(n%3!=0){
return false;
}
double val = Math.log(n) / Math.log(3);
double rounded = (double) Math.round(val * 10000) / 10000;
if (Math.ceil(val) - rounded == 0) {
return true;
}
return false;
}
```