-
-
Notifications
You must be signed in to change notification settings - Fork 50.2k
feat: add Karatsuba multiplication algorithm in divide_and_conquer folder #13298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """ | ||
| Performs multiplication of two binary strings using the Karatsuba algorithm. | ||
|
|
||
| Given two binary strings of equal length n, the goal is to compute | ||
| their product as integers. | ||
|
|
||
| For example: | ||
| "1100" (12) × "1010" (10) = 120 | ||
|
|
||
| Karatsuba's algorithm reduces the multiplication of two n-bit numbers | ||
| to at most three multiplications of n/2-bit numbers. It achieves | ||
| a time complexity of O(n^log₂3) ≈ O(n^1.585), which is faster | ||
| than the naive O(n²) approach. | ||
|
|
||
| References: | ||
| https://en.wikipedia.org/wiki/Karatsuba_algorithm | ||
|
|
||
| Example: | ||
| >>> karatsuba_multiply("1100", "1010") | ||
| 120 | ||
| >>> karatsuba_multiply("10", "11") | ||
| 6 | ||
| >>> karatsuba_multiply("101", "111") | ||
| 35 | ||
| >>> karatsuba_multiply("0", "0") | ||
| 0 | ||
| >>> karatsuba_multiply("1", "0") | ||
| 0 | ||
| """ | ||
|
|
||
|
|
||
| def karatsuba_multiply(x: str, y: str) -> int: | ||
| """ | ||
| Multiplies two binary strings using the Karatsuba algorithm. | ||
|
|
||
| Both input strings should be of equal length. | ||
|
|
||
| >>> karatsuba_multiply("1100", "1010") | ||
| 120 | ||
| >>> karatsuba_multiply("11", "10") | ||
| 6 | ||
| >>> karatsuba_multiply("1111", "1111") | ||
| 225 | ||
| """ | ||
| n = max(len(x), len(y)) | ||
|
|
||
| # Pad the shorter string with leading zeros | ||
| x = x.zfill(n) | ||
| y = y.zfill(n) | ||
|
|
||
| # Base case: single bit multiplication | ||
| if n == 1: | ||
| return int(x) * int(y) | ||
|
|
||
| mid = n // 2 | ||
|
|
||
| # Split the binary strings into left and right halves | ||
| x_left, x_right = x[:mid], x[mid:] | ||
| y_left, y_right = y[:mid], y[mid:] | ||
|
|
||
| # Recursively compute partial products | ||
| p1 = karatsuba_multiply(x_left, y_left) | ||
| p2 = karatsuba_multiply(x_right, y_right) | ||
| p3 = karatsuba_multiply( | ||
| bin(int(x_left, 2) + int(x_right, 2))[2:], | ||
| bin(int(y_left, 2) + int(y_right, 2))[2:], | ||
| ) | ||
|
|
||
| # Karatsuba combination formula: | ||
| result = (p1 << (2 * (n - mid))) + ((p3 - p1 - p2) << (n - mid)) + p2 | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
xPlease provide descriptive name for the parameter:
y