One line solution using mod


  • 7
    Q
    public class Solution326_3 {
        public boolean isPowerOfThree(int n) {
            return n > 0 && 1162261467 % n == 0;
        }
    }

  • 0
    R

    Can you explain this code (slightly in detail if possible)? Thank you very much! :)


  • 0
    D

    1162261467 is the largest power of 3 that can fit into an integer datatype.
    1162261467 = 3^19 + 0

    The given n can be expressed as [(a power of 3) + (some x)]. I think it is fairly elementary to be able to prove that if x is 0(which happens iff n is a power of 3), 1162261467 % n = 0.

    Beautiful solution :)


  • 0
    R

    Thank you very much! :)


  • 0

    How did you get 1162261467 in the first place?


Log in to reply
 

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