Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
323 views
1
# euclid.py
2
#
3
# Author: Ralph Gootee <[email protected]>
4
#
5
# implementaions of the euclidean algorithm and the extended
6
# euclidean algorithm
7
#
8
9
from math import floor;
10
11
#-------------------------------------------------------------------------------
12
def gcd(a,b):
13
while b != 0:
14
t = b;
15
b = a % b;
16
a = t;
17
return a
18
19
#-------------------------------------------------------------------------------
20
def xgcd(a,b):
21
22
x = 0;
23
y = 1;
24
lastx = 1;
25
lasty = 0;
26
27
while b != 0:
28
29
temp = b;
30
quotient = int(floor(a / b));
31
b = a % b;
32
a = temp;
33
temp = x;
34
x = lastx -quotient*x;
35
lastx = temp;
36
temp = y;
37
y = lasty-quotient*y;
38
lasty = temp;
39
40
return [lastx,lasty];
41
42
43