原题:
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
对于类似的题可以找到一个通用的解法,即假maxPower是32位整数中,3的最大次幂, 那么任意3的幂都应该是它的约数,任意不是3的幂的整数都不是它的约数。
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
if n<=0:
return False
import math
maxInteger=0xffffffff
return math.pow(3,int(math.log(maxInteger,3))) % n==0