We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e2a78d4 commit 09f27a6Copy full SHA for 09f27a6
maths/is_power_of_four.py
@@ -0,0 +1,37 @@
1
+"""
2
+Check if an integer is a power of four.
3
+
4
+Time Complexity: O(log₄ n)
5
+Space Complexity: O(1)
6
7
8
+def is_power_of_four(n: int) -> bool:
9
+ """Return True if n is a power of four (n > 0).
10
11
+ >>> is_power_of_four(1)
12
+ True
13
+ >>> is_power_of_four(4)
14
15
+ >>> is_power_of_four(16)
16
17
+ >>> is_power_of_four(64)
18
19
+ >>> is_power_of_four(8)
20
+ False
21
+ >>> is_power_of_four(0)
22
23
+ >>> is_power_of_four(-4)
24
25
+ """
26
+ if not isinstance(n, int):
27
+ raise TypeError("n must be an integer")
28
+ if n <= 0:
29
+ return False
30
+ while n % 4 == 0:
31
+ n //= 4
32
+ return n == 1
33
34
35
+if __name__ == "__main__":
36
+ import doctest
37
+ doctest.testmod()
0 commit comments