mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
* 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 <dhruvmanila@gmail.com>
This commit is contained in:
parent
a5000d32ed
commit
e41d04112f
@ -3,59 +3,72 @@ Extended Euclidean Algorithm.
|
|||||||
|
|
||||||
Finds 2 numbers a and b such that it satisfies
|
Finds 2 numbers a and b such that it satisfies
|
||||||
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
||||||
|
|
||||||
|
https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm
|
||||||
"""
|
"""
|
||||||
|
|
||||||
# @Author: S. Sharma <silentcat>
|
# @Author: S. Sharma <silentcat>
|
||||||
# @Date: 2019-02-25T12:08:53-06:00
|
# @Date: 2019-02-25T12:08:53-06:00
|
||||||
# @Email: silentcat@protonmail.com
|
# @Email: silentcat@protonmail.com
|
||||||
# @Last modified by: PatOnTheBack
|
# @Last modified by: pikulet
|
||||||
# @Last modified time: 2019-07-05
|
# @Last modified time: 2020-10-02
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Tuple
|
||||||
|
|
||||||
|
|
||||||
def extended_euclidean_algorithm(m, n):
|
def extended_euclidean_algorithm(a: int, b: int) -> Tuple[int, int]:
|
||||||
"""
|
"""
|
||||||
Extended Euclidean Algorithm.
|
Extended Euclidean Algorithm.
|
||||||
|
|
||||||
Finds 2 numbers a and b such that it satisfies
|
Finds 2 numbers a and b such that it satisfies
|
||||||
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
the equation am + bn = gcd(m, n) (a.k.a Bezout's Identity)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(1, 24)
|
||||||
|
(1, 0)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(8, 14)
|
||||||
|
(2, -1)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(240, 46)
|
||||||
|
(-9, 47)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(1, -4)
|
||||||
|
(1, 0)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(-2, -4)
|
||||||
|
(-1, 0)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(0, -4)
|
||||||
|
(0, -1)
|
||||||
|
|
||||||
|
>>> extended_euclidean_algorithm(2, 0)
|
||||||
|
(1, 0)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
a = 0
|
# base cases
|
||||||
a_prime = 1
|
if abs(a) == 1:
|
||||||
b = 1
|
return a, 0
|
||||||
b_prime = 0
|
elif abs(b) == 1:
|
||||||
q = 0
|
return 0, b
|
||||||
r = 0
|
|
||||||
if m > n:
|
|
||||||
c = m
|
|
||||||
d = n
|
|
||||||
else:
|
|
||||||
c = n
|
|
||||||
d = m
|
|
||||||
|
|
||||||
while True:
|
old_remainder, remainder = a, b
|
||||||
q = int(c / d)
|
old_coeff_a, coeff_a = 1, 0
|
||||||
r = c % d
|
old_coeff_b, coeff_b = 0, 1
|
||||||
if r == 0:
|
|
||||||
break
|
|
||||||
c = d
|
|
||||||
d = r
|
|
||||||
|
|
||||||
t = a_prime
|
while remainder != 0:
|
||||||
a_prime = a
|
quotient = old_remainder // remainder
|
||||||
a = t - q * a
|
old_remainder, remainder = remainder, old_remainder - quotient * remainder
|
||||||
|
old_coeff_a, coeff_a = coeff_a, old_coeff_a - quotient * coeff_a
|
||||||
|
old_coeff_b, coeff_b = coeff_b, old_coeff_b - quotient * coeff_b
|
||||||
|
|
||||||
t = b_prime
|
# sign correction for negative numbers
|
||||||
b_prime = b
|
if a < 0:
|
||||||
b = t - q * b
|
old_coeff_a = -old_coeff_a
|
||||||
|
if b < 0:
|
||||||
|
old_coeff_b = -old_coeff_b
|
||||||
|
|
||||||
pair = None
|
return old_coeff_a, old_coeff_b
|
||||||
if m > n:
|
|
||||||
pair = (a, b)
|
|
||||||
else:
|
|
||||||
pair = (b, a)
|
|
||||||
return pair
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
@ -63,9 +76,9 @@ def main():
|
|||||||
if len(sys.argv) < 3:
|
if len(sys.argv) < 3:
|
||||||
print("2 integer arguments required")
|
print("2 integer arguments required")
|
||||||
exit(1)
|
exit(1)
|
||||||
m = int(sys.argv[1])
|
a = int(sys.argv[1])
|
||||||
n = int(sys.argv[2])
|
b = int(sys.argv[2])
|
||||||
print(extended_euclidean_algorithm(m, n))
|
print(extended_euclidean_algorithm(a, b))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user