forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathis_power_of_four.py
45 lines (35 loc) · 982 Bytes
/
is_power_of_four.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
Author :- mehul-sweeti-agrawal
Task :- Given an integer N, find whether that integer is a power of 4 or not.
Input - N
Output - Yes/No
"""
def is_power_of_four(N: int) -> bool:
"""
Returns whether a number is a power of 4 or not
>>> is_power_of_four(8)
False
>>> is_power_of_four(4)
True
>>> is_power_of_four(16)
True
>>> is_power_of_four(-1)
Traceback (most recent call last):
...
ValueError: number must be positive
>>> is_power_of_four(9.8)
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for &: 'float' and 'float'
"""
# For non-positive numbers
if N <= 0:
raise ValueError("number must be positive")
# If number is a power of 2 and ends with 4 or 6
if (N & (N - 1) == 0) and (N % 10 == 6 or N % 10 == 4):
return True
else:
return False
if __name__ == "__main__":
import doctest
doctest.testmod()