-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Changing Name of file and adding doctests in file. #9513
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
Merged
tianyizheng02
merged 13 commits into
TheAlgorithms:master
from
AasheeshLikePanner:master
Oct 3, 2023
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
2c4994c
Adding doctests and changing file name
AasheeshLikePanner 90e5eb8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 85f2cc1
Update binary_multiplication.py
AasheeshLikePanner dd16da3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ff50144
Update binary_multiplication.py
AasheeshLikePanner c20642e
Changing comment and changing name function
AasheeshLikePanner a9d007d
Merge remote-tracking branch 'origin/master'
AasheeshLikePanner 4b6dfbf
Changing comment and changing name function
AasheeshLikePanner 6c0e81f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 629eba0
Update binary_multiplication.py
AasheeshLikePanner 7570ac6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] caadd29
Update binary_multiplication.py
tianyizheng02 be6c8b1
Update binary_multiplication.py
tianyizheng02 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 was deleted.
Oops, something went wrong.
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,101 @@ | ||
""" | ||
Binary Multiplication | ||
This is a method to find a*b in a time complexity of O(log b) | ||
This is one of the most commonly used methods of finding result of multiplication. | ||
Also useful in cases where solution to (a*b)%c is required, | ||
where a,b,c can be numbers over the computers calculation limits. | ||
Done using iteration, can also be done using recursion | ||
|
||
Let's say you need to calculate a * b | ||
RULE 1 : a * b = (a+a) * (b/2) ---- example : 4 * 4 = (4+4) * (4/2) = 8 * 2 | ||
RULE 2 : IF b is odd, then ---- a * b = a + (a * (b - 1)), where (b - 1) is even. | ||
Once b is even, repeat the process to get a * b | ||
Repeat the process until b = 1 or b = 0, because a*1 = a and a*0 = 0 | ||
|
||
As far as the modulo is concerned, | ||
the fact : (a+b) % c = ((a%c) + (b%c)) % c | ||
Now apply RULE 1 or 2, whichever is required. | ||
|
||
@author chinmoy159 | ||
""" | ||
|
||
|
||
def binary_multiply(a: int, b: int) -> int: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: Please provide descriptive name for the parameter: |
||
""" | ||
Multiply 'a' and 'b' using bitwise multiplication. | ||
|
||
Parameters: | ||
a (int): The first number. | ||
b (int): The second number. | ||
|
||
Returns: | ||
int: a * b | ||
|
||
Examples: | ||
>>> binary_multiply(2, 3) | ||
6 | ||
>>> binary_multiply(5, 0) | ||
0 | ||
>>> binary_multiply(3, 4) | ||
12 | ||
>>> binary_multiply(10, 5) | ||
50 | ||
>>> binary_multiply(0, 5) | ||
0 | ||
>>> binary_multiply(2, 1) | ||
2 | ||
>>> binary_multiply(1, 10) | ||
10 | ||
""" | ||
res = 0 | ||
while b > 0: | ||
if b & 1: | ||
res += a | ||
|
||
a += a | ||
b >>= 1 | ||
|
||
return res | ||
|
||
|
||
def binary_mod_multiply(a: int, b: int, modulus: int) -> int: | ||
AasheeshLikePanner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Calculate (a * b) % c using binary multiplication and modular arithmetic. | ||
|
||
Parameters: | ||
a (int): The first number. | ||
b (int): The second number. | ||
modulus (int): The modulus. | ||
|
||
Returns: | ||
int: (a * b) % modulus. | ||
|
||
Examples: | ||
>>> binary_mod_multiply(2, 3, 5) | ||
1 | ||
>>> binary_mod_multiply(5, 0, 7) | ||
0 | ||
>>> binary_mod_multiply(3, 4, 6) | ||
0 | ||
>>> binary_mod_multiply(10, 5, 13) | ||
11 | ||
>>> binary_mod_multiply(2, 1, 5) | ||
2 | ||
>>> binary_mod_multiply(1, 10, 3) | ||
1 | ||
""" | ||
res = 0 | ||
while b > 0: | ||
if b & 1: | ||
res = ((res % modulus) + (a % modulus)) % modulus | ||
|
||
a += a | ||
b >>= 1 | ||
|
||
return res | ||
|
||
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.