aboutsummaryrefslogtreecommitdiff
path: root/power_of_four.py
blob: da9ab09b15271efb90bc4efa167d4f8a2f0073ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 342. Power of Four

"""
given an integer 'n', return 'true' if it is a power of four, otherwise,
return 'false'. an integer 'n' is a power of four if there exists an integer
'x' such that 'n == 4^x'
"""


class Solution(object):
    def isPowerOfFour(self, n):
        return n > 0 and n.bit_count() == 1 and (n - 1) % 3 == 0


if __name__ == "__main__":
    obj = Solution()
    print(obj.isPowerOfFour(16))  # expect: True
    print(obj.isPowerOfFour(5))  # expect: False
    print(obj.isPowerOfFour(1))  # expect: True