|
| 1 | +""" |
| 2 | +Binary Multiplication |
| 3 | +This is a method to find a*b in a time complexity of O(log b) |
| 4 | +This is one of the most commonly used methods of finding result of multiplication. |
| 5 | +Also useful in cases where solution to (a*b)%c is required, |
| 6 | +where a,b,c can be numbers over the computers calculation limits. |
| 7 | +Done using iteration, can also be done using recursion |
| 8 | +
|
| 9 | +Let's say you need to calculate a * b |
| 10 | +RULE 1 : a * b = (a+a) * (b/2) ---- example : 4 * 4 = (4+4) * (4/2) = 8 * 2 |
| 11 | +RULE 2 : IF b is odd, then ---- a * b = a + (a * (b - 1)), where (b - 1) is even. |
| 12 | +Once b is even, repeat the process to get a * b |
| 13 | +Repeat the process until b = 1 or b = 0, because a*1 = a and a*0 = 0 |
| 14 | +
|
| 15 | +As far as the modulo is concerned, |
| 16 | +the fact : (a+b) % c = ((a%c) + (b%c)) % c |
| 17 | +Now apply RULE 1 or 2, whichever is required. |
| 18 | +
|
| 19 | +@author chinmoy159 |
| 20 | +""" |
| 21 | + |
| 22 | + |
| 23 | +def binary_multiply(a: int, b: int) -> int: |
| 24 | + """ |
| 25 | + Multiply 'a' and 'b' using bitwise multiplication. |
| 26 | +
|
| 27 | + Parameters: |
| 28 | + a (int): The first number. |
| 29 | + b (int): The second number. |
| 30 | +
|
| 31 | + Returns: |
| 32 | + int: a * b |
| 33 | +
|
| 34 | + Examples: |
| 35 | + >>> binary_multiply(2, 3) |
| 36 | + 6 |
| 37 | + >>> binary_multiply(5, 0) |
| 38 | + 0 |
| 39 | + >>> binary_multiply(3, 4) |
| 40 | + 12 |
| 41 | + >>> binary_multiply(10, 5) |
| 42 | + 50 |
| 43 | + >>> binary_multiply(0, 5) |
| 44 | + 0 |
| 45 | + >>> binary_multiply(2, 1) |
| 46 | + 2 |
| 47 | + >>> binary_multiply(1, 10) |
| 48 | + 10 |
| 49 | + """ |
| 50 | + res = 0 |
| 51 | + while b > 0: |
| 52 | + if b & 1: |
| 53 | + res += a |
| 54 | + |
| 55 | + a += a |
| 56 | + b >>= 1 |
| 57 | + |
| 58 | + return res |
| 59 | + |
| 60 | + |
| 61 | +def binary_mod_multiply(a: int, b: int, modulus: int) -> int: |
| 62 | + """ |
| 63 | + Calculate (a * b) % c using binary multiplication and modular arithmetic. |
| 64 | +
|
| 65 | + Parameters: |
| 66 | + a (int): The first number. |
| 67 | + b (int): The second number. |
| 68 | + modulus (int): The modulus. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + int: (a * b) % modulus. |
| 72 | +
|
| 73 | + Examples: |
| 74 | + >>> binary_mod_multiply(2, 3, 5) |
| 75 | + 1 |
| 76 | + >>> binary_mod_multiply(5, 0, 7) |
| 77 | + 0 |
| 78 | + >>> binary_mod_multiply(3, 4, 6) |
| 79 | + 0 |
| 80 | + >>> binary_mod_multiply(10, 5, 13) |
| 81 | + 11 |
| 82 | + >>> binary_mod_multiply(2, 1, 5) |
| 83 | + 2 |
| 84 | + >>> binary_mod_multiply(1, 10, 3) |
| 85 | + 1 |
| 86 | + """ |
| 87 | + res = 0 |
| 88 | + while b > 0: |
| 89 | + if b & 1: |
| 90 | + res = ((res % modulus) + (a % modulus)) % modulus |
| 91 | + |
| 92 | + a += a |
| 93 | + b >>= 1 |
| 94 | + |
| 95 | + return res |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + import doctest |
| 100 | + |
| 101 | + doctest.testmod() |
0 commit comments