public static boolean isPowerOfThree(int n) {
if (n <= 0)
return false;
double r = Math.log10(n) / Math.log10(3);
if (r % 1 == 0)
return true;
else
return false;
}
JAVAEasy Version To Understand!!!!!!!!!!!

To avoid round off error a small twist
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; }