Power of Three

Given an integer, write a function to determine if it is a power of three.


Follow up:
Could you do it without using any loop / recursion?

Credits:Special thanks to @dietpepsi for adding this problem and creating all test cases.

Solution

public class Solution {
  public boolean isPowerOfThree(int n) {
         if(n <=0) return false;
        int logNum = (int) (Math.log(n) / Math.log(3));
              if (Math.pow(3, logNum) == n || Math.pow(3, logNum+1) == n) return true;

        else return false;
    }
}

results matching ""

    No results matching ""