-
Notifications
You must be signed in to change notification settings - Fork 0
/
euclidean.py
47 lines (32 loc) · 820 Bytes
/
euclidean.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# simple algorithm
def euclidean (a,b):
'''
ax + by = 1, returns (x,y)
'''
# a = mb + c
m = a // b
c = a - m * b
if (c == 1):
return (1, -m)
else:
Q,P = euclidean (b, c)
return (P, Q - P * m)
def euclideanPrint (a,b):
'''
ax + by = 1, returns (x,y)
Prints result after each step
'''
# a = mb + c
m = a // b
c = a - m * b
print('{} = {}({}) + {}'.format(a,m,b,c))
if (c == 1):
print('\nSUBSTITUTE\n\n1 = 1({}) - {}({})'.format(a,m,b))
return (1, -m)
else:
Q,P = euclideanPrint (b, c)
Qn, Pn = P, Q - P * m
print('{} = {}({}) + {}({})'.format(Qn*a + Pn*b, Qn, a, Pn, b))
return (Qn, Pn)
if __name__ == '__main__':
print(euclideanPrint(157,73))