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?
Python:
class Solution(object):
def isPowerOfThree(self, n):
"""
:type n: int
:rtype: bool
"""
#循环
if n <= 0:
return False
while n % 3 == 0:
n = n / 3
return n == 1
#非循环
#>>> math.log(243,3)
#4.999999999999999
if n <= 0:
return False
import sys
import math
max_num = pow(3, int(math.log(sys.maxint, 3)))
return max_num % n == 0