|
1 | 1 | """ |
2 | | -Performs multiplication of two binary strings using the Karatsuba algorithm. |
3 | | -
|
4 | | -Given two binary strings of equal length n, the goal is to compute |
5 | | -their product as integers. |
6 | | -
|
7 | | -For example: |
8 | | -"1100" (12) × "1010" (10) = 120 |
9 | | -
|
10 | | -Karatsuba's algorithm reduces the multiplication of two n-bit numbers |
11 | | -to at most three multiplications of n/2-bit numbers. It achieves |
12 | | -a time complexity of O(n^log₂3) ≈ O(n^1.585), which is faster |
13 | | -than the naive O(n²) approach. |
14 | | -
|
15 | | -References: |
16 | | -https://en.wikipedia.org/wiki/Karatsuba_algorithm |
| 2 | +Karatsuba Multiplication Algorithm |
| 3 | +---------------------------------- |
| 4 | +Given two binary strings that represent the value of two integers, |
| 5 | +find the product of the two strings using the Karatsuba algorithm. |
17 | 6 |
|
18 | 7 | Example: |
19 | | ->>> karatsuba_multiply("1100", "1010") |
20 | | -120 |
21 | | ->>> karatsuba_multiply("10", "11") |
22 | | -6 |
23 | | ->>> karatsuba_multiply("101", "111") |
24 | | -35 |
25 | | ->>> karatsuba_multiply("0", "0") |
26 | | -0 |
27 | | ->>> karatsuba_multiply("1", "0") |
28 | | -0 |
| 8 | + "1100" (12) x "1010" (10) = 120 |
29 | 9 | """ |
30 | 10 |
|
31 | 11 |
|
32 | | -def karatsuba_multiply(binary_str_1: str, binary_str_2: str) -> int: |
| 12 | +def karatsuba_multiply(bin_num1: str, bin_num2: str) -> int: |
33 | 13 | """ |
34 | | - Multiplies two binary strings using the Karatsuba algorithm. |
35 | | -
|
36 | | - Both input strings should be of equal length. |
37 | | -
|
38 | | - >>> karatsuba_multiply("1100", "1010") |
39 | | - 120 |
40 | | - >>> karatsuba_multiply("11", "10") |
41 | | - 6 |
42 | | - >>> karatsuba_multiply("1111", "1111") |
43 | | - 225 |
| 14 | + Multiply two binary strings using the Karatsuba algorithm. |
| 15 | +
|
| 16 | + Args: |
| 17 | + bin_num1 (str): The first binary string. |
| 18 | + bin_num2 (str): The second binary string. |
| 19 | +
|
| 20 | + Returns: |
| 21 | + int: The product of the two binary strings as an integer. |
| 22 | +
|
| 23 | + Examples: |
| 24 | + >>> karatsuba_multiply("1100", "1010") |
| 25 | + 120 |
| 26 | + >>> karatsuba_multiply("11", "11") |
| 27 | + 9 |
| 28 | + >>> karatsuba_multiply("101", "10") |
| 29 | + 10 |
44 | 30 | """ |
45 | | - n = max(len(binary_str_1), len(binary_str_2)) |
46 | | - |
47 | | - # Pad the shorter string with leading zeros |
48 | | - binary_str_1 = binary_str_1.zfill(n) |
49 | | - binary_str_2 = binary_str_2.zfill(n) |
50 | | - |
51 | | - # Base case: single bit multiplication |
52 | | - if n == 1: |
53 | | - return int(binary_str_1) * int(binary_str_2) |
54 | | - |
55 | | - mid = n // 2 |
56 | | - |
57 | | - # Split the binary strings into left and right halves |
58 | | - left_1, right_1 = binary_str_1[:mid], binary_str_1[mid:] |
59 | | - left_2, right_2 = binary_str_2[:mid], binary_str_2[mid:] |
60 | | - |
61 | | - # Recursively compute partial products |
62 | | - product_left = karatsuba_multiply(left_1, left_2) |
63 | | - product_right = karatsuba_multiply(right_1, right_2) |
64 | | - product_sum = karatsuba_multiply( |
65 | | - bin(int(left_1, 2) + int(right_1, 2))[2:], |
66 | | - bin(int(left_2, 2) + int(right_2, 2))[2:], |
67 | | - ) |
68 | | - |
69 | | - # Karatsuba combination formula |
70 | | - result = ( |
71 | | - (product_left << (2 * (n - mid))) |
72 | | - + ((product_sum - product_left - product_right) << (n - mid)) |
73 | | - + product_right |
74 | | - ) |
75 | | - |
76 | | - return result |
| 31 | + # Convert binary strings to integers |
| 32 | + x = int(bin_num1, 2) |
| 33 | + y = int(bin_num2, 2) |
| 34 | + |
| 35 | + # Base case for recursion |
| 36 | + if x < 2 or y < 2: |
| 37 | + return x * y |
| 38 | + |
| 39 | + # Calculate the size of the numbers |
| 40 | + n = max(len(bin_num1), len(bin_num2)) |
| 41 | + half = n // 2 |
| 42 | + |
| 43 | + # Split the binary strings |
| 44 | + x_high = x >> half |
| 45 | + x_low = x & ((1 << half) - 1) |
| 46 | + y_high = y >> half |
| 47 | + y_low = y & ((1 << half) - 1) |
| 48 | + |
| 49 | + # Recursive multiplications |
| 50 | + a = karatsuba_multiply(bin(x_high)[2:], bin(y_high)[2:]) |
| 51 | + d = karatsuba_multiply(bin(x_low)[2:], bin(y_low)[2:]) |
| 52 | + e = karatsuba_multiply(bin(x_high + x_low)[2:], bin(y_high + y_low)[2:]) - a - d |
| 53 | + |
| 54 | + # Combine results |
| 55 | + return (a << (2 * half)) + (e << half) + d |
77 | 56 |
|
78 | 57 |
|
79 | 58 | if __name__ == "__main__": |
|
0 commit comments