Skip to content

Commit 47d5f34

Browse files
committed
feat(math): is power of two
1 parent 1f89788 commit 47d5f34

12 files changed

+100
-1
lines changed

pymath/power_of_i/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
Description:
1+
# Power of I
2+
23
iii is the imaginary unit, it is defined by i²=−1i² = -1i²=−1, therefore it is a solution to x²+1=0x² + 1 = 0x²+1=0.
34

45
Your Task Complete the function pofi that returns iii to the power of a given non-negative integer in its simplest form,

pymath/power_of_two/README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Power of Two
2+
3+
Given an integer n, return true if it is a power of two. Otherwise, return false.
4+
5+
An integer n is a power of two, if there exists an integer x such that n == 2x.
6+
7+
## Examples
8+
9+
Example 1:
10+
```text
11+
Input: n = 1
12+
Output: true
13+
Explanation: 2^0 = 1
14+
```
15+
16+
Example 2:
17+
```text
18+
Input: n = 16
19+
Output: true
20+
Explanation: 2^4 = 16
21+
```
22+
23+
Example 3:
24+
25+
```text
26+
Input: n = 3
27+
Output: false
28+
```
29+
30+
## Constraints
31+
32+
- -2^31 <= n <= 2^31 - 1
33+
34+
35+
> Follow up: Could you solve it without loops/recursion?
36+
37+
## Solution
38+
39+
The main idea of this solution is to use bit manipulation to check whether a number contains only one set bit in its
40+
binary representation. Powers of two are always positive and have exactly one bit set to 1, while all other bits are 0.
41+
First, verify that the number is greater than zero, as negative numbers and zero cannot be powers of two. Then, use the
42+
property that for any power of two, subtracting 1 from n turns the rightmost 1-bit into 0 and flips all bits to the right
43+
of it to 1. Performing a bitwise AND between n and n - 1 gives zero only for powers of two, confirming that the number
44+
contains exactly one set bit.
45+
46+
![Binary Representation](./images/solutions/power_of_two_binary_representation.png)
47+
48+
Using the intuition above, we implement the algorithm as follows:
49+
50+
- If n is less than or equal to zero, return FALSE.
51+
- Compute n - 1 to flip all bits after the rightmost 1-bit in the binary representation of n.
52+
- Perform a bitwise AND between n and n - 1.
53+
- Return TRUE if the result of the above computation is 0; FALSE otherwise.
54+
55+
Let’s look at the following illustration to get a better understanding of the solution:
56+
57+
![Solution 1](./images/solutions/power_of_two_solution_1.png)
58+
![Solution 2](./images/solutions/power_of_two_solution_2.png)
59+
![Solution 3](./images/solutions/power_of_two_solution_3.png)
60+
![Solution 4](./images/solutions/power_of_two_solution_4.png)
61+
![Solution 5](./images/solutions/power_of_two_solution_5.png)
62+
![Solution 6](./images/solutions/power_of_two_solution_6.png)
63+
64+
### Time Complexity
65+
66+
The algorithm’s time complexity is O(1) because it performs only a fixed number of arithmetic and bitwise operations,
67+
regardless of the value of n.
68+
69+
### Space Complexity
70+
71+
The algorithm’s space complexity is constant, O(1).

pymath/power_of_two/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def is_power_of_two(n: int) -> bool:
2+
# Perform bitwise AND between n and (n - 1)
3+
# This will be 0 only if n has exactly one 1-bit in its binary form
4+
return n > 0 and (n & (n - 1)) == 0
18.4 KB
Loading
11 KB
Loading
8.89 KB
Loading
7.18 KB
Loading
16.5 KB
Loading
29.3 KB
Loading

0 commit comments

Comments
 (0)