forked from ruppysuppy/Daily-Coding-Problem-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path268.py
More file actions
31 lines (22 loc) · 634 Bytes
/
268.py
File metadata and controls
31 lines (22 loc) · 634 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Problem:
Given a 32-bit positive integer N, determine whether it is a power of four in faster
than O(log N) time.
"""
# for details visit: https://stackoverflow.com/a/19611541/8650340
def is_power_of_4(num: int) -> bool:
return ((num & -num) & 0x55555554) == num
if __name__ == "__main__":
print(is_power_of_4(2))
print(is_power_of_4(4)) # 4 ^ 1
print(is_power_of_4(8))
print(is_power_of_4(16)) # 4 ^ 2
print(is_power_of_4(32))
print(is_power_of_4(64)) # 4 ^ 3
print(is_power_of_4(128))
print(is_power_of_4(256)) # 4 ^ 4
"""
SPECS:
TIME COMPLEXITY: O(1)
SPACE COMPLEXITY: O(1)
"""