The main idea is checking whether log(n, 3) is an integer.
def isPowerOfThree(self, n):
return n > 0 and abs(math.log(n, 3) - round(math.log(n, 3))) < 1e-10
Thanks for this. I went the same route but was having trouble because I was using sys.float_info.epsilon, which was making certain test cases fail.
Glad to help you :)
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.