public boolean isPowerOfThree(int n){
  if (n <= 0)   return false;
  while (n > 1 && n % 3 == 0) {
    n/=3;
  }
  return n == 1;
}

public boolean isPowerOfThree ( int n ) { if ( n <= 0 ) return false ; while ( n > 1 && n % 3 == 0 ) { n /= 3 ; } return n == 1 ; }
check if n ' s modular of 3 is zero . if yes , divide n by 3 and check again . stop till n is 1 .
