The Chinese Remainder Theorem
The Chinese Remainder Theorem is using to solve systems of congruences with different moduli. This states that, for any and coprime natural numbers , as well as integers , the so-called simultaneous congruences (in below) have a solution, and all possible solutions of this congruence system are congruent modulo the product .
def crt(a, n):
assert len(a) == len(n), "len(a) != len(n)"
N = reduce((lambda x, y: x * y), n)
x = 0
for j in range(len(n)):
N_j = N / n[j]
s_j = xgcd(N_j, n[j])[1]
x = (x + a[j] * s_j * N_j) % N
return x
Sage example:
CRT_list([4,1,3,0], [7,3,5,11])
Complexity:
Last updated