Python solution using math (probably slow)


  • 0
    E

    The log base 4 of num is only integral when num is a power of four.

    Given any log function you can calculate log base b of n by log(n)/log(b). This is floating point math using a (probably?) slow math function. (Pulling log4 out as a separate function also has significant overhead In Python)

    Leetcode didn't give me my performance stats because there had not been enough submissions...

    from math import log
    
    l4 = log(4)
    
    def log4(n):
        return log(n)/l4
    
    class Solution(object):
        def isPowerOfFour(self, num):
            if num <= 0:
                return False
            return log4(num)%1 == 0

Log in to reply
 

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