Java one line solution


  • 8
    J
    public boolean isPowerOfThree(int n) {
       // The expression "(int) Math.pow(3, (int) (Math.log(Integer.MAX_VALUE) / Math.log(3.0))" returns max integer that is "power of 3"
        return n > 0 && (int) Math.pow(3, (int) (Math.log(Integer.MAX_VALUE) / Math.log(3.0))) % n == 0;
    }

  • -8
    T

    very good, the idea is awesome, hopefully it would be added into jdk in the future.


  • 0
    J

    Briliant idea, but you can set the max signed integer, which is power of 3, 1162261467 in code, just like this:

    return n > 0 && 1162261467 % n == 0;
    

    Then we do not need to calculate the max "power of 3" every time.


  • 0
    E

    How do you determine that 1162261467 is the largest result of a power of 3?


  • 0
    J

    @EbukaObizoba largest int number is 2,147,483,647 (inclusive)


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.