""" 0342.1 - Power of Four - Solution 1 - Iterative Division """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPowerOfFour(self, n: int) -> bool:
"""Power of Four Function"""
if n <= 0:
return False
while n % 4 == 0:
n //= 4
return n == 1
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPowerOfFour(16)) # True
print(Solution().isPowerOfFour(5)) # False
print(Solution().isPowerOfFour(1)) # True
print(Solution().isPowerOfFour(0)) # False
print(Solution().isPowerOfFour(-4)) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0342.2 - Power of Four - Solution 2 - Bit Manipulation """
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPowerOfFour(self, n: int) -> bool:
"""Power of Four Function"""
return n > 0 and (n & (n - 1)) == 0 and (n & 0x55555555) != 0
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPowerOfFour(16)) # True
print(Solution().isPowerOfFour(5)) # False
print(Solution().isPowerOfFour(1)) # True
print(Solution().isPowerOfFour(8)) # False (power of 2, not 4)
print(Solution().isPowerOfFour(64)) # True
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()
""" 0342.3 - Power of Four - Solution 3 - Math (Logarithm) """
#####################################################################################
# Imports
#####################################################################################
import math
#####################################################################################
# Classes
#####################################################################################
class Solution:
"""Solution Class"""
def isPowerOfFour(self, n: int) -> bool:
"""Power of Four Function"""
if n <= 0:
return False
return math.log2(n) % 2 == 0
#####################################################################################
# Functions
#####################################################################################
def testcase():
"""Test Function"""
print(Solution().isPowerOfFour(16)) # True
print(Solution().isPowerOfFour(5)) # False
print(Solution().isPowerOfFour(1)) # True
print(Solution().isPowerOfFour(64)) # True
print(Solution().isPowerOfFour(32)) # False
#####################################################################################
# Main
#####################################################################################
if __name__ == "__main__":
testcase()