Bug in the python processing? Believe solution is valid but getting failure on 243

  • 0
    from math import log
    class Solution(object):
        def isPowerOfThree(self, n):
            :type n: int
            :rtype: bool
            if n <= 0:
                return False
            if log(n, 3).is_integer():
                return True
            return False

    This is driving me nuts, one of the easy ways to check if a number is a power of (x) is take log(n)base(x) and see if you've got an integer... well for some reason this code is returning false for 243. I printed the output, I get 5.0 in stdout... am I running into some floating point number precision issue?

  • 0

    I got a run time error when import math module...

  • 1

    math.log(243,3) gives 4.999999999999999 in python.

Log in to reply

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