Skip to content

Commit e41d041

Browse files
pikuletdhruvmanila
andauthored
Fixes: #2630 Add doctests and support for negative numbers (#2626)
* add type hints to math/extended euclid * math/extended euclid - add doctest * math/extended euclid: remove manual doctest * change algorithm for negative numbers * improve naming of variables * Update extended_euclidean_algorithm.py Co-authored-by: Dhruv <[email protected]>
1 parent a5000d3 commit e41d041

File tree

1 file changed

+54
-41
lines changed

1 file changed

+54
-41
lines changed

Diff for: maths/extended_euclidean_algorithm.py

+54-41
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,82 @@
33
44
Finds 2 numbers a and b such that it satisfies
55
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
6+
7+
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
68
"""
79

810
# @Author: S. Sharma <silentcat>
911
# @Date: 2019-02-25T12:08:53-06:00
1012
11-
# @Last modified by: PatOnTheBack
12-
# @Last modified time: 2019-07-05
13+
# @Last modified by: pikulet
14+
# @Last modified time: 2020-10-02
1315

1416
import sys
17+
from typing import Tuple
1518

1619

17-
def extended_euclidean_algorithm(m, n):
20+
def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]:
1821
"""
1922
Extended Euclidean Algorithm.
2023
2124
Finds 2 numbers a and b such that it satisfies
2225
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
26+
27+
>>> extended_euclidean_algorithm(1, 24)
28+
(1, 0)
29+
30+
>>> extended_euclidean_algorithm(8, 14)
31+
(2, -1)
32+
33+
>>> extended_euclidean_algorithm(240, 46)
34+
(-9, 47)
35+
36+
>>> extended_euclidean_algorithm(1, -4)
37+
(1, 0)
38+
39+
>>> extended_euclidean_algorithm(-2, -4)
40+
(-1, 0)
41+
42+
>>> extended_euclidean_algorithm(0, -4)
43+
(0, -1)
44+
45+
>>> extended_euclidean_algorithm(2, 0)
46+
(1, 0)
47+
2348
"""
24-
a = 0
25-
a_prime = 1
26-
b = 1
27-
b_prime = 0
28-
q = 0
29-
r = 0
30-
if m > n:
31-
c = m
32-
d = n
33-
else:
34-
c = n
35-
d = m
36-
37-
while True:
38-
q = int(c / d)
39-
r = c % d
40-
if r == 0:
41-
break
42-
c = d
43-
d = r
44-
45-
t = a_prime
46-
a_prime = a
47-
a = t - q * a
48-
49-
t = b_prime
50-
b_prime = b
51-
b = t - q * b
52-
53-
pair = None
54-
if m > n:
55-
pair = (a, b)
56-
else:
57-
pair = (b, a)
58-
return pair
49+
# base cases
50+
if abs(a) == 1:
51+
return a, 0
52+
elif abs(b) == 1:
53+
return 0, b
54+
55+
old_remainder, remainder = a, b
56+
old_coeff_a, coeff_a = 1, 0
57+
old_coeff_b, coeff_b = 0, 1
58+
59+
while remainder != 0:
60+
quotient = old_remainder // remainder
61+
old_remainder, remainder = remainder, old_remainder - quotient * remainder
62+
old_coeff_a, coeff_a = coeff_a, old_coeff_a - quotient * coeff_a
63+
old_coeff_b, coeff_b = coeff_b, old_coeff_b - quotient * coeff_b
64+
65+
# sign correction for negative numbers
66+
if a < 0:
67+
old_coeff_a = -old_coeff_a
68+
if b < 0:
69+
old_coeff_b = -old_coeff_b
70+
71+
return old_coeff_a, old_coeff_b
5972

6073

6174
def main():
6275
"""Call Extended Euclidean Algorithm."""
6376
if len(sys.argv) < 3:
6477
print("2 integer arguments required")
6578
exit(1)
66-
m = int(sys.argv[1])
67-
n = int(sys.argv[2])
68-
print(extended_euclidean_algorithm(m, n))
79+
a = int(sys.argv[1])
80+
b = int(sys.argv[2])
81+
print(extended_euclidean_algorithm(a, b))
6982

7083

7184
if __name__ == "__main__":

0 commit comments

Comments
 (0)