From 2c4994cb09c6b77c379412e79445283943d49acb Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Mon, 2 Oct 2023 22:54:16 +0530 Subject: [PATCH 01/12] Adding doctests and changing file name --- ...ntiation_2.py => binary_multiplication.py} | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) rename maths/{binary_exponentiation_2.py => binary_multiplication.py} (51%) diff --git a/maths/binary_exponentiation_2.py b/maths/binary_multiplication.py similarity index 51% rename from maths/binary_exponentiation_2.py rename to maths/binary_multiplication.py index af8f776dd266..ee7dd95c3da5 100644 --- a/maths/binary_exponentiation_2.py +++ b/maths/binary_multiplication.py @@ -12,6 +12,32 @@ def b_expo(a: int, b: int) -> int: + """ + Calculate the result of multiplying 'a' and 'b' using bitwise multiplication. + + Parameters: + a (int): The first number. + b (int): The second number. + + Returns: + int: The result of 'a' multiplied by 'b'. + + Examples: + >>> b_expo(2, 3) + 6 + >>> b_expo(5, 0) + 0 + >>> b_expo(3, 4) + 12 + >>> b_expo(10, 5) + 50 + >>> b_expo(0, 5) + 0 + >>> b_expo(2, 1) + 2 + >>> b_expo(1, 10) + 10 + """ res = 0 while b > 0: if b & 1: @@ -24,6 +50,35 @@ def b_expo(a: int, b: int) -> int: def b_expo_mod(a: int, b: int, c: int) -> int: + """ + Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic. + + Parameters: + a (int): The first number. + b (int): The second number. + c (int): The modulus. + + Returns: + int: The result of (a * b) % c. + + Examples: + >>> b_expo_mod(2, 3, 5) + 1 + >>> b_expo_mod(5, 0, 7) + 0 + >>> b_expo_mod(3, 4, 6) + 0 + >>> b_expo_mod(10, 5, 13) + 8 + >>> b_expo_mod(2, 1, 5) + 2 + >>> b_expo_mod(1, 10, 3) + 1 + >>> b_expo_mod(7, 3, 4) + 1 + >>> b_expo_mod(8, 2, 10) + 6 + """ res = 0 while b > 0: if b & 1: @@ -34,6 +89,9 @@ def b_expo_mod(a: int, b: int, c: int) -> int: return res +if __name__ == "__main__": + import doctest + doctest.testmod() """ * Wondering how this method works ! From 90e5eb8cf1d40962ca8c63f1b0cb3c1d80af8317 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 2 Oct 2023 17:26:40 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binary_multiplication.py | 108 +++++++++++++++++---------------- 1 file changed, 55 insertions(+), 53 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index ee7dd95c3da5..573ae615de2e 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -13,31 +13,31 @@ def b_expo(a: int, b: int) -> int: """ - Calculate the result of multiplying 'a' and 'b' using bitwise multiplication. - - Parameters: - a (int): The first number. - b (int): The second number. - - Returns: - int: The result of 'a' multiplied by 'b'. - - Examples: - >>> b_expo(2, 3) - 6 - >>> b_expo(5, 0) - 0 - >>> b_expo(3, 4) - 12 - >>> b_expo(10, 5) - 50 - >>> b_expo(0, 5) - 0 - >>> b_expo(2, 1) - 2 - >>> b_expo(1, 10) - 10 - """ + Calculate the result of multiplying 'a' and 'b' using bitwise multiplication. + + Parameters: + a (int): The first number. + b (int): The second number. + + Returns: + int: The result of 'a' multiplied by 'b'. + + Examples: + >>> b_expo(2, 3) + 6 + >>> b_expo(5, 0) + 0 + >>> b_expo(3, 4) + 12 + >>> b_expo(10, 5) + 50 + >>> b_expo(0, 5) + 0 + >>> b_expo(2, 1) + 2 + >>> b_expo(1, 10) + 10 + """ res = 0 while b > 0: if b & 1: @@ -51,34 +51,34 @@ def b_expo(a: int, b: int) -> int: def b_expo_mod(a: int, b: int, c: int) -> int: """ - Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic. - - Parameters: - a (int): The first number. - b (int): The second number. - c (int): The modulus. - - Returns: - int: The result of (a * b) % c. - - Examples: - >>> b_expo_mod(2, 3, 5) - 1 - >>> b_expo_mod(5, 0, 7) - 0 - >>> b_expo_mod(3, 4, 6) - 0 - >>> b_expo_mod(10, 5, 13) - 8 - >>> b_expo_mod(2, 1, 5) - 2 - >>> b_expo_mod(1, 10, 3) - 1 - >>> b_expo_mod(7, 3, 4) - 1 - >>> b_expo_mod(8, 2, 10) - 6 - """ + Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic. + + Parameters: + a (int): The first number. + b (int): The second number. + c (int): The modulus. + + Returns: + int: The result of (a * b) % c. + + Examples: + >>> b_expo_mod(2, 3, 5) + 1 + >>> b_expo_mod(5, 0, 7) + 0 + >>> b_expo_mod(3, 4, 6) + 0 + >>> b_expo_mod(10, 5, 13) + 8 + >>> b_expo_mod(2, 1, 5) + 2 + >>> b_expo_mod(1, 10, 3) + 1 + >>> b_expo_mod(7, 3, 4) + 1 + >>> b_expo_mod(8, 2, 10) + 6 + """ res = 0 while b > 0: if b & 1: @@ -89,8 +89,10 @@ def b_expo_mod(a: int, b: int, c: int) -> int: return res + if __name__ == "__main__": import doctest + doctest.testmod() """ From 85f2cc1a3e90564c2b8257568acb41830aee4d46 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:15:08 +0530 Subject: [PATCH 03/12] Update binary_multiplication.py --- maths/binary_multiplication.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 573ae615de2e..8769316f7370 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -51,7 +51,7 @@ def b_expo(a: int, b: int) -> int: def b_expo_mod(a: int, b: int, c: int) -> int: """ - Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic. + Calculate (a * b) % c using binary exponentiation and modular arithmetic. Parameters: a (int): The first number. @@ -74,10 +74,6 @@ def b_expo_mod(a: int, b: int, c: int) -> int: 2 >>> b_expo_mod(1, 10, 3) 1 - >>> b_expo_mod(7, 3, 4) - 1 - >>> b_expo_mod(8, 2, 10) - 6 """ res = 0 while b > 0: @@ -92,7 +88,6 @@ def b_expo_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() """ From dd16da3a74d00af727071b599c04353ae6c82343 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 10:47:13 +0000 Subject: [PATCH 04/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binary_multiplication.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 8769316f7370..fda4669ea493 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -88,6 +88,7 @@ def b_expo_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() """ From ff50144aad5723c877f61a1896c95526f13b9028 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Tue, 3 Oct 2023 16:20:18 +0530 Subject: [PATCH 05/12] Update binary_multiplication.py --- maths/binary_multiplication.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index fda4669ea493..1be5b93048a2 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -69,7 +69,7 @@ def b_expo_mod(a: int, b: int, c: int) -> int: >>> b_expo_mod(3, 4, 6) 0 >>> b_expo_mod(10, 5, 13) - 8 + 11 >>> b_expo_mod(2, 1, 5) 2 >>> b_expo_mod(1, 10, 3) From c20642e277ea76de32546b5d1c02fdc79b6f96a4 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Tue, 3 Oct 2023 19:51:46 +0530 Subject: [PATCH 06/12] Changing comment and changing name function --- maths/binary_multiplication.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index ee7dd95c3da5..f58f21a7ddd5 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -1,5 +1,5 @@ """ -* Binary Exponentiation with Multiplication +* Binary Multiplication with 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, @@ -11,7 +11,7 @@ """ -def b_expo(a: int, b: int) -> int: +def b_multi(a: int, b: int) -> int: """ Calculate the result of multiplying 'a' and 'b' using bitwise multiplication. @@ -49,7 +49,7 @@ def b_expo(a: int, b: int) -> int: return res -def b_expo_mod(a: int, b: int, c: int) -> int: +def b_multi_mod(a: int, b: int, c: int) -> int: """ Calculate the result of (a * b) % c using binary exponentiation and modular arithmetic. From 4b6dfbfe37fb48e31ee3aa9df9696f6c49afb539 Mon Sep 17 00:00:00 2001 From: AasheeshLikePanner Date: Tue, 3 Oct 2023 20:07:44 +0530 Subject: [PATCH 07/12] Changing comment and changing name function --- maths/binary_multiplication.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 4a7a06d2e68c..b0d12b8776f0 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -23,19 +23,19 @@ def b_multi(a: int, b: int) -> int: int: The result of 'a' multiplied by 'b'. Examples: - >>> b_expo(2, 3) + >>> b_multi(2, 3) 6 - >>> b_expo(5, 0) + >>> b_multi(5, 0) 0 - >>> b_expo(3, 4) + >>> b_multi(3, 4) 12 - >>> b_expo(10, 5) + >>> b_multi(10, 5) 50 - >>> b_expo(0, 5) + >>> b_multi(0, 5) 0 - >>> b_expo(2, 1) + >>> b_multi(2, 1) 2 - >>> b_expo(1, 10) + >>> b_multi(1, 10) 10 """ res = 0 @@ -62,17 +62,17 @@ def b_multi_mod(a: int, b: int, c: int) -> int: int: The result of (a * b) % c. Examples: - >>> b_expo_mod(2, 3, 5) + >>> b_multi_mod(2, 3, 5) 1 - >>> b_expo_mod(5, 0, 7) + >>> b_multi_mod(5, 0, 7) 0 - >>> b_expo_mod(3, 4, 6) + >>> b_multi_mod(3, 4, 6) 0 - >>> b_expo_mod(10, 5, 13) + >>> b_multi_mod(10, 5, 13) 11 - >>> b_expo_mod(2, 1, 5) + >>> b_multi_mod(2, 1, 5) 2 - >>> b_expo_mod(1, 10, 3) + >>> b_multi_mod(1, 10, 3) 1 """ res = 0 @@ -88,7 +88,6 @@ def b_multi_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() """ From 6c0e81faf735c627508ff60c0e669ee92d231151 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:38:25 +0000 Subject: [PATCH 08/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binary_multiplication.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index b0d12b8776f0..3c768b8a5075 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -88,6 +88,7 @@ def b_multi_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() """ From 629eba0598f14911464bbd927e02c06accc7a1f4 Mon Sep 17 00:00:00 2001 From: Aasheesh <126905285+AasheeshLikePanner@users.noreply.github.com> Date: Tue, 3 Oct 2023 20:10:38 +0530 Subject: [PATCH 09/12] Update binary_multiplication.py --- maths/binary_multiplication.py | 1 - 1 file changed, 1 deletion(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 3c768b8a5075..b0d12b8776f0 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -88,7 +88,6 @@ def b_multi_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest - doctest.testmod() """ From 7570ac6318d626b05f125c99df21ca7af298f832 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 3 Oct 2023 14:41:26 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/binary_multiplication.py | 1 + 1 file changed, 1 insertion(+) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index b0d12b8776f0..3c768b8a5075 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -88,6 +88,7 @@ def b_multi_mod(a: int, b: int, c: int) -> int: if __name__ == "__main__": import doctest + doctest.testmod() """ From caadd29b25fec4accbb23a5a4ab2d26176d06fbe Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 3 Oct 2023 11:30:55 -0400 Subject: [PATCH 11/12] Update binary_multiplication.py --- maths/binary_multiplication.py | 79 ++++++++++++++++------------------ 1 file changed, 37 insertions(+), 42 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 3c768b8a5075..73b4894cbe63 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -1,41 +1,50 @@ """ -* Binary Multiplication with 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 - -* @author chinmoy159 -* @version 1.0 dated 10/08/2017 +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 b_multi(a: int, b: int) -> int: +def binary_multiply(a: int, b: int) -> int: """ - Calculate the result of multiplying 'a' and 'b' using bitwise multiplication. + Multiply 'a' and 'b' using bitwise multiplication. Parameters: a (int): The first number. b (int): The second number. Returns: - int: The result of 'a' multiplied by 'b'. + int: a * b Examples: - >>> b_multi(2, 3) + >>> binary_multiply(2, 3) 6 - >>> b_multi(5, 0) + >>> binary_multiply(5, 0) 0 - >>> b_multi(3, 4) + >>> binary_multiply(3, 4) 12 - >>> b_multi(10, 5) + >>> binary_multiply(10, 5) 50 - >>> b_multi(0, 5) + >>> binary_multiply(0, 5) 0 - >>> b_multi(2, 1) + >>> binary_multiply(2, 1) 2 - >>> b_multi(1, 10) + >>> binary_multiply(1, 10) 10 """ res = 0 @@ -49,9 +58,9 @@ def b_multi(a: int, b: int) -> int: return res -def b_multi_mod(a: int, b: int, c: int) -> int: +def binary_mod_multiply(a: int, b: int, c: int) -> int: """ - Calculate (a * b) % c using binary exponentiation and modular arithmetic. + Calculate (a * b) % c using binary multiplication and modular arithmetic. Parameters: a (int): The first number. @@ -59,20 +68,20 @@ def b_multi_mod(a: int, b: int, c: int) -> int: c (int): The modulus. Returns: - int: The result of (a * b) % c. + int: (a * b) % c. Examples: - >>> b_multi_mod(2, 3, 5) + >>> binary_mod_multiply(2, 3, 5) 1 - >>> b_multi_mod(5, 0, 7) + >>> binary_mod_multiply(5, 0, 7) 0 - >>> b_multi_mod(3, 4, 6) + >>> binary_mod_multiply(3, 4, 6) 0 - >>> b_multi_mod(10, 5, 13) + >>> binary_mod_multiply(10, 5, 13) 11 - >>> b_multi_mod(2, 1, 5) + >>> binary_mod_multiply(2, 1, 5) 2 - >>> b_multi_mod(1, 10, 3) + >>> binary_mod_multiply(1, 10, 3) 1 """ res = 0 @@ -90,17 +99,3 @@ def b_multi_mod(a: int, b: int, c: int) -> int: import doctest doctest.testmod() - -""" -* Wondering how this method works ! -* It's pretty simple. -* 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 till 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. -""" From be6c8b18d71870829dd16c5401db1264e90d934e Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Tue, 3 Oct 2023 11:35:36 -0400 Subject: [PATCH 12/12] Update binary_multiplication.py --- maths/binary_multiplication.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/maths/binary_multiplication.py b/maths/binary_multiplication.py index 73b4894cbe63..0cc5a575f445 100644 --- a/maths/binary_multiplication.py +++ b/maths/binary_multiplication.py @@ -58,17 +58,17 @@ def binary_multiply(a: int, b: int) -> int: return res -def binary_mod_multiply(a: int, b: int, c: int) -> int: +def binary_mod_multiply(a: int, b: int, modulus: int) -> int: """ Calculate (a * b) % c using binary multiplication and modular arithmetic. Parameters: a (int): The first number. b (int): The second number. - c (int): The modulus. + modulus (int): The modulus. Returns: - int: (a * b) % c. + int: (a * b) % modulus. Examples: >>> binary_mod_multiply(2, 3, 5) @@ -87,7 +87,7 @@ def binary_mod_multiply(a: int, b: int, c: int) -> int: res = 0 while b > 0: if b & 1: - res = ((res % c) + (a % c)) % c + res = ((res % modulus) + (a % modulus)) % modulus a += a b >>= 1