|
3 | 3 |
|
4 | 4 | Finds 2 numbers a and b such that it satisfies
|
5 | 5 | the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
| 6 | +
|
| 7 | +https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm |
6 | 8 | """
|
7 | 9 |
|
8 | 10 | # @Author: S. Sharma <silentcat>
|
9 | 11 | # @Date: 2019-02-25T12:08:53-06:00
|
10 | 12 |
|
11 |
| -# @Last modified by: PatOnTheBack |
12 |
| -# @Last modified time: 2019-07-05 |
| 13 | +# @Last modified by: pikulet |
| 14 | +# @Last modified time: 2020-10-02 |
13 | 15 |
|
14 | 16 | import sys
|
| 17 | +from typing import Tuple |
15 | 18 |
|
16 | 19 |
|
17 |
| -def extended_euclidean_algorithm(m, n): |
| 20 | +def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]: |
18 | 21 | """
|
19 | 22 | Extended Euclidean Algorithm.
|
20 | 23 |
|
21 | 24 | Finds 2 numbers a and b such that it satisfies
|
22 | 25 | 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 | +
|
23 | 48 | """
|
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 |
59 | 72 |
|
60 | 73 |
|
61 | 74 | def main():
|
62 | 75 | """Call Extended Euclidean Algorithm."""
|
63 | 76 | if len(sys.argv) < 3:
|
64 | 77 | print("2 integer arguments required")
|
65 | 78 | 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)) |
69 | 82 |
|
70 | 83 |
|
71 | 84 | if __name__ == "__main__":
|
|
0 commit comments