### 7.1.20 Chinese remainders

Theorem 1 (Chinese remainders) If p1,p2,…,pn are relatively prime, then for any integers a1,a2,…,an there is a number c such that c≡ a1(mod p1 ), c≡ a2 (mod p2 ), …, c≡ an (mod pn ).

The ichinrem or ichrem command finds this value of c.

- ichinrem takes one or more arguments: Each argument is a pair of integers ak and pk either as a list [ak,pk] or as a modular integer ak% pk.
- ichinrem([a1,p1],[a2,p2],…,[an,pn]) if possible returns a list [c,L], where L= lcm(p1,p2,…,pn) and c satisfies c≡ ak (mod pk ) for k=1,…,n.

Note that any multiple of L=lcm(p1,p2,…,pn) can be added to c and the equalities will still be true. If the pk are relatively prime, then a solution c exists by Theorem 1; what’s more, any two solutions will be congruent modulo the product of the pks.

If all of the arguments are given as modular integers, then the result will also be given as a modular integer c % l.

The chrem command does the same thing as ichinrem, but the input is given in a different form.

- chrem takes two arguments: [a1,…,an] and [p1,…,pn], lists of integers of the same size.
- chrem([a1,…,an],[p1,…,pn]) returns [c,L], as for ichinrem.

Be careful with the order of the parameters. Indeed, chrem([a,b],[p,q]), ichrem([a,p],[b,q]) and ichinrem([a,p],[b,q]) all produce the same output.

#### Examples

Solve:

x | ≡ 3(mod 5 ) |  |  |  |  |  |  |  |  |
x | ≡ 9(mod 13 ) |  |  |  |  |  |  |  |  |

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

or:

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

| ⎡ ⎣ | 48,65 | ⎤ ⎦

so x≡ 48 (mod 65 ).

You can also input:

ichrem(3%5,9%13)

| ⎛ ⎝ | −17 | ⎞ ⎠ | %65

Note that 48≡ −17 (mod 65 ).

Recalling that chrem takes its arguments in a different form, you can also enter:

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

| ⎡ ⎣ | 48,65 | ⎤ ⎦

Solve:

x | ≡ 3(mod 5 ) |  |  |  |  |  |  |  |  |
x | ≡ 4(mod 7 ) |  |  |  |  |  |  |  |  |
x | ≡ 1(mod 9 ) |  |  |  |  |  |  |  |  |

ichinrem([3,5],[4,7],[1,9])

| ⎡ ⎣ | 298,315 | ⎤ ⎦

hence x≡ 298 (mod 315 ).

Alternative input:

ichinrem([3%5,4%7,1%9])

| ⎛ ⎝ | −17 | ⎞ ⎠ | %315

Note that 298≡ −17 (mod 315 ).

Again, with the arguments in a different form, you can also enter:

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

| ⎡ ⎣ | 298,315 | ⎤ ⎦

##### Remark.

These three commands, ichinrem, ichrem and chrem, may also be used to find the coefficients of a polynomial whose equivalence classes are known modulo several integers by using polynomials with integer coefficients instead of integers for the ak.

For example, to find ax+b modulo 315=5 · 7 · 9 under the assumptions

a | ≡ 3(mod 5 ) |  |  |  |  |  |  |  |  |
a | ≡ 4(mod 7 ) |  |  |  |  |  |  |  |  |
a | ≡ 1(mod 9 ) |  |  |  |  |  |  |  |  |

and

b | ≡ 1(mod 5 ) |  |  |  |  |  |  |  |  |
b | ≡ 2(mod 7 ) |  |  |  |  |  |  |  |  |
b | ≡ 3(mod 9 ) |  |  |  |  |  |  |  |  |

#### Example

ichinrem((3 x+1)%5,(4 x+2)%7,(x+3)%9)

| ⎛ ⎝ | ⎛ ⎝ | −17 | ⎞ ⎠ | %315 | ⎞ ⎠ | x+156%315

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

As before, chrem takes the same input in a different format.

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

| ⎡ ⎣ | 298 x+156,315 | ⎤ ⎦

Note that 298≡ −17 (mod 315 ).