Problem link. This is simple, just make sure you don’t overflow (I do it with the q > 0 check).
q > 0
public boolean isPowerOfThree(int n) { int q = 1; for(int i = 1; (q > 0 && q <= n); i++) { if (q == n) return true; q *= 3; } return false; }