# Without log and O(1).

• ``````class Solution {
public:
int const Max3PowerInt = 1162261467; // 3^19, 3^20 = 3486784401 > MaxInt32
int const MaxInt32 = 2147483647; // 2^31 - 1
bool isPowerOfThree(int n) {
if (n <= 0 || n > Max3PowerInt) return false;
return Max3PowerInt % n == 0;
}
};
``````

Typically, Log(x, y) is not O(1), it should be O(ln(N)), which just hides the loop/recursion .

• good idea!
but get value of max3PowerInt also need a program.

• !~ NICE this idea is really good

• your MaxInt is declared but not used. And it is not Max Int either.

• but, % as modulo operator, it is itself a iterative function. I think we can never truly get rid of such loop for this problem, if we take into consideration the hidden loops/recursions/iterations.

• very nice and clear

• Excellent!!!

• % modulo operator can be implemented in O(1) using bit operations.

• if(n<=0)
return false;
return (INT_MAX/3)%n==0;

27 is not pass,why?

• @awplxz You have to use Max3PowerInt not INT_MAX.
Try this

``````    double d = 3;
int maxp3 = 1;
while (d < Integer.MAX_VALUE ) {
maxp3 *= 3;
d *= 3;
}
int x =  maxp3% 27;``````

• @gruber you use java,my solution base on C++,thank you al the same~

• nice solution

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