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?

Solution

public class Solution {
    public boolean isPowerOfThree(int n) {
        if(n <= 0) return false;
        int max = (int)Math.pow( 3, (int)( Math.log(Integer.MAX_VALUE) / Math.log(3) ) );
        return max % n == 0;
    }
}

results matching ""

    No results matching ""