Skip to content

Commit 493ffc8

Browse files
aniakubikabranhe
authored andcommitted
add extended euclidean algorithm
1 parent 9c4c5e5 commit 493ffc8

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

math/extendedEuclidean.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
"""
2+
for numbers a, b returns x, y such as x * a + y * b = gcd(a,b)
3+
"""
4+
5+
def extendedEuclidean(a,b):
6+
a_old, b_old = a,b
7+
a, b = max(a, b), min(a, b)
8+
x, y, old_x, old_y = 0, 1, 1, 0
9+
while b != 0:
10+
quotient = a // b
11+
residue = a % b
12+
a, b = b, residue
13+
x, old_x = old_x, (old_x - quotient * x)
14+
y, old_y = old_y, (old_y - quotient * y)
15+
if a_old > b_old:
16+
return x, y
17+
return y, x
18+
19+
20+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy