-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathxgcd-crt.sage
83 lines (64 loc) · 1.69 KB
/
xgcd-crt.sage
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def crt(a: list[int], n: list[int]):
"""
Chinese Remainder Theorem demonstration.
"""
assert len(a) == len(n)
# find N as multiplication of all n
N = 1
for n_i in n:
N *= n_i
# find x'
xp = 0
for i in range(len(a)):
Ndiv = N // n[i]
_, s, t = xgcd(Ndiv, n[i])
xp += a[i] * s * Ndiv
# return x' mod N
return xp % N
def xgcd(a: int, b: int, verbose: bool = False):
"""
Extended Euclidean Algorithm
Given `a` and `b`, finds `gcd(a,b)`, `s`
and `t` such that `gcd(a, b) = s * a + t * b`.
Parameters:
- `a` number
- `b` number
- `verbose` print the execution trace
Returns:
- `[gcd(a,b), s, t]` as a triple
"""
assert a >= b
# initials
r = [a, b]
s = [1, 0]
t = [0, 1]
k = 2
while r[k - 1] != 0:
quot = r[k - 2] // r[k - 1]
rem = r[k - 2] % r[k - 1]
r.append(rem)
s.append(s[k - 2] - quot * s[k - 1])
t.append(t[k - 2] - quot * t[k - 1])
k += 1
if verbose:
print("{0}|\t{1}\t{2}\t{3}".format("k", "r", "s", "t"))
for i in range(k):
print("{0}|\t{1}\t{2}\t{3}".format(i, r[i], s[i], t[i]))
return r[k - 2], s[k - 2], t[k - 2]
def xgcd_arrayless(a: int, b: int):
assert a >= b
r_prev, r_cur = a, b
s_prev, s_cur = 1, 0
t_prev, t_cur = 0, 1
while r_cur != 0:
quot = r_prev // r_cur
rem = r_prev % r_cur
s_next = s_prev - quot * s_cur
t_next = t_prev - quot * t_cur
r_prev = r_cur
r_cur = rem
s_prev = s_cur
s_cur = s_next
t_prev = t_cur
t_cur = t_next
return r_prev, s_prev, t_prev