Power of 3
Question
不用任何Loop和recursion,check一个number是否是3的power
Thoughts
This can be generalized for any prime number.
Let's 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
而最大的32bit signed interger是1162261467
Solution
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
return (n > 0) and (1162261467 % n == 0)