# 1 line java solution without loop / recursion

• ``````public class Solution {
public boolean isPowerOfThree(int n) {
// 1162261467 is 3^19,  3^20 is bigger than int
return ( n>0 &&  1162261467%n==0);
}
``````

}

• Very Very impressive. I couldn't think of this at all.

• How could 3^19 % 3^2 be equal to 0? I thought 3^19 % 3^2 should be 3.

• 3^19 = 3^17 * 3^2
why should it be 3?

• Very impressive. If this question is twisted a bit to check for powerOfFour, would you still compute the absolute value and figure out which power of 4 falls outside the max value of int?

• @srivatsa: for pow of 4, take sqrt and check if it's power of 2, which is trivial.

• its 0, 3^19/3^2 = 3^17

• How to be sure that only power of 3 can get 0 after modulo?

• if n is power of three, then the multiplier of n is also the power of three.So the modulo would be zero definitely. If any part of the number not belonging to the power of three, the modulo will be non-zero.

• So clever!!!

• excellent ! I learn a lot from this

• very good!!!

• so clever!!! very good

• wow！awesome！

• Yes, 3^19 = 3^17 * 3^2, but 3^19 % 3^2 = 3^17 % 3^2 = 3^15 % 3^2 = ... = 3 % 3^2 = 3?

• @xiaokecong 3^3 % 3^2 = 3, dude...

• @MtAdams Man, you're very impressive too ...

3^19 / 3^2 = 3^17, which is a whole number, which means there's no remainder, which is, if interpreted into math language, 3^19 % 3^2 = 0.

• So clever! This is valid for all prime numbers `p` (`p > 2`), right?

• @Hcisly Good point. Yes, it is.

• This post is deleted!

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