### 5.6.23 Chinese remainders for lists of integers : chrem

chrem takes as argument 2 lists of integers of the same size.
chrem returns a list of 2 integers.
For example, chrem([a,b,c],[p,q,r]) returns the list [x,lcm(p,q,r)] where x=a mod p and x=b mod q and x=c mod r.
A solution x always exists if p, q, r are mutually primes, and all the solutions are equal modulo p*q*r.
Be careful with the order of the parameters, indeed :
chrem([a,b],[p,q])=ichrem([a,p],[b,q])=
ichinrem([a,p],[b,q])
Examples :
Solve :

⎧ ⎨ ⎩ |
x= | 3 (mod 5)
x= | 9 (mod 13)

Input :

chrem([3,9],[5,13])

Output :

[-17,65]

so, x=-17 (mod 65)
Solve :

⎧ ⎪ ⎨ ⎪ ⎩ |
x= | 3 (mod 5)
x= | 4 (mod 6)
x= | 1 (mod 9)

Input :

chrem([3,4,1],[5,6,9])

Output :

[28,90]

so x=28 (mod 90)
Remark
chrem may be used to find the coefficients of a polynomial whose equivalence classes are known modulo several integers, for example find ax+b modulo 315=5 × 7 × 9 under the assumptions:

⎧ ⎪ ⎨ ⎪ ⎩ |
a= | 3 (mod 5)
a= | 4 (mod 7)
a= | 1 (mod 9)
| , |
⎧ ⎪ ⎨ ⎪ ⎩ |
b= | 1 (mod 5)
b= | 2 (mod 7)
b= | 3 (mod 9)

Input :

chrem([3x+1,4x+2,x+3],[5,7,9])

Output :

[-17x+156,315]

hence, a=-17 (mod 315) and b=156 (mod 315).