Chinese Remainder Theorem (CRT)

Given N=i=1kniN = \prod_{i=1}^{k}n_i where each moduli(or divisor) nin_i is a pairwise coprime, and the set of aia_i is given as below:

xa1(modn1)xak(modnk)x \equiv a_1 \pmod {n_1} \\ \vdots \\ x \equiv a_k \pmod {n_k}

Then the system has a unique solution xx under modulo NN such that:

xs1i1ni+s2i2ni++skikni(modN)x \equiv s_1 \cdot \prod_{i \ne 1} n_i + s_2 \cdot \prod_{i \ne 2} n_i + \cdots + s_k \cdot \prod_{i \ne k} n_i \pmod N

where each sis_i satisfies:

sijinj=ai(modni)s_i \cdot \prod_{j \ne i} n_j = a_i \pmod {n_i}

In other words, a set of modulus statements can be reduced to a single statement.

Take the example below:

x={2(mod3)3(mod5)2(mod7)x = \begin{cases} 2 \pmod 3 \\ 3 \pmod 5 \\ 2 \pmod 7 \end{cases}
35s12s12(mod3)21s2s23(mod5)15s3s32(mod7)35 \cdot s_1 \equiv 2 \cdot s_1 \equiv 2 \pmod 3 \\ 21 \cdot s_2 \equiv s_2 \equiv 3 \pmod 5 \\ 15 \cdot s_3 \equiv s_3 \equiv 2 \pmod 7 \\

Solving these gives s1=1,s2=3s_1 = 1, s_2 = 3 and s3=2s_3 = 2. Then the solution is

x135+321+21512823(mod105)x \equiv 1 \cdot 35 + 3 \cdot 21 + 2 \cdot 15 \equiv 128 \equiv 23 \pmod {105}

Written by ryan Kim from A41

Last updated