You can also view more solution on Github
class Solution {
public:
bool isPowerOfThree(int n) {
return n>0 && 1162261467%n == 0;
}
};
You can also view more solution on Github
class Solution {
public:
bool isPowerOfThree(int n) {
return n>0 && 1162261467%n == 0;
}
};
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.
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.