One-line solution


  • 0
    Z

    You can also view more solution on Github

    class Solution {
    public:
        bool isPowerOfThree(int n) {
            return n>0 && 1162261467%n == 0;
        }
    };

  • 0

    How you determined 1162261467%n==0 will always be a pow of 3? I am not clear with this. Can you please explain it


  • 0
    S

    that number is just a super large integer that is a power of three (3 raised to the 19th to be exact) so basically that number mod any power of 3 will return 0 (because a power of 3 will divide that super large number with no remainder) and hence why the solution works. it wouldn't work mathematically speaking for powers of 3 larger than 3^19 so its sort of a whack solution in my opinion.


  • 0
    L

    Good job on the "whack" part, I just tried giving it an additional power of 3 in python and it is successfully calculated, not sure why people call it the "largest" number that is power of 3.


  • 0
    J

    Because the number 1162261467 is the largest integer which is a power of 3 in java. The max integer value is (2^31)-1 = 2147483647.


  • 0
    S

    im not sure if you're responding to what i said but remember that i prefaced my statement with "mathematically speaking"; however, java does support integers larger than 32 bits, it has 64 bit long integer type as well as the BigInteger class, which is bounded by the ram on your computer. So stating that 2^31 - 1 is the largest integer in java is sort of a half truth. its the largest int type.


  • 0

    How did you get 1162261467 in the first place without using a loop?


Log in to reply
 

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