From 0b686a3a2419e2b81f1e719616a9a3295588234f Mon Sep 17 00:00:00 2001 From: aniakubik Date: Mon, 21 Oct 2019 20:54:12 +0200 Subject: [PATCH 1/2] add extended euclidean algorithm --- math/extendedEuclidean.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 math/extendedEuclidean.py diff --git a/math/extendedEuclidean.py b/math/extendedEuclidean.py new file mode 100644 index 0000000..f220a37 --- /dev/null +++ b/math/extendedEuclidean.py @@ -0,0 +1,20 @@ +""" + for numbers a, b returns x, y such as x * a + y * b = gcd(a,b) +""" + +def extendedEuclidean(a,b): + a_old, b_old = a,b + a, b = max(a, b), min(a, b) + x, y, old_x, old_y = 0, 1, 1, 0 + while b != 0: + quotient = a // b + residue = a % b + a, b = b, residue + x, old_x = old_x, (old_x - quotient * x) + y, old_y = old_y, (old_y - quotient * y) + if a_old > b_old: + return x, y + return y, x + + + From 8cacca6c20eb4279251c66c9981bcd2a7aafdee6 Mon Sep 17 00:00:00 2001 From: aniakubik Date: Fri, 25 Oct 2019 17:38:05 +0200 Subject: [PATCH 2/2] fix bug --- math/extendedEuclidean.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/math/extendedEuclidean.py b/math/extendedEuclidean.py index f220a37..a1f0845 100644 --- a/math/extendedEuclidean.py +++ b/math/extendedEuclidean.py @@ -5,16 +5,16 @@ def extendedEuclidean(a,b): a_old, b_old = a,b a, b = max(a, b), min(a, b) - x, y, old_x, old_y = 0, 1, 1, 0 + x, y, old_x, old_y = 1, 0, 0, 1 while b != 0: quotient = a // b residue = a % b a, b = b, residue - x, old_x = old_x, (old_x - quotient * x) - y, old_y = old_y, (old_y - quotient * y) - if a_old > b_old: - return x, y - return y, x + old_x, x = x, (old_x - quotient * x) + old_y, y = y, (old_y - quotient * y) + if b_old > a_old: + return old_x, old_y + return old_y, old_x