# O(1) C Solution

• ``````This can be generalized for any prime number n.
Lets say we have 2 number m & n.
If m is a power of n then for any number p,
1.  For all p<=m
2.  m%p = 0 if and only if p is also a power of n
We can use this concept here also. In this case n=3 and m is largest 32bit signed integer which is power of 3 i.e. 1162261467.

bool isPowerOfThree(int p) {
return  p>0 && 1162261467%p == 0 ;
}``````

• "This can be generalized for any number."

How can it be generalized to powers of 6? The way you describe it doesn't work, as for example 36%2 is 0, but 2 is not a power of 6.

• Ohhh I see.....I think it can be generalized for only prime n values.
Thanks for pointing this out.

• Yeah, maybe there's a way to adapt it for composites, but I don't see it.

• This post is deleted!

• The best answer I've ever seen

• This can only be generalized for prime numbers.

• For a given prime number n, how would you compute the largest 32 bit integer which is a power of n?
Don't you need a loop for this?

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