Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 493ffc8

Browse filesBrowse files
aniakubikabranhe
authored andcommitted
add extended euclidean algorithm
1 parent 9c4c5e5 commit 493ffc8
Copy full SHA for 493ffc8

File tree

Expand file treeCollapse file tree

1 file changed

+20
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+20
-0
lines changed

‎math/extendedEuclidean.py

Copy file name to clipboard
+20Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.