All pages
Powered by GitBook
1 of 1

Chinese Remainder Theorem (CRT)

Given N=∏i=1kniN = \prod_{i=1}^{k}n_iN=∏i=1k​ni​ where each moduli(or divisor) nin_ini​ is a pairwise coprime, and the set of aia_iai​ is given as below:

x≡a1(modn1)⋮x≡ak(modnk)x \equiv a_1 \pmod {n_1} \\ \vdots \\ x \equiv a_k \pmod {n_k}x≡a1​(modn1​)⋮x≡ak​(modnk​)

Then the system has a unique solution xxx under modulo NNN such that:

x≡s1⋅∏i≠1ni+s2⋅∏i≠2ni+⋯+sk⋅∏i≠kni(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 Nx≡s1​⋅i=1∏​ni​+s2​⋅i=2∏​ni​+⋯+sk​⋅i=k∏​ni​(modN)

where each sis_isi​ satisfies:

si⋅∏j≠inj=ai(modni)s_i \cdot \prod_{j \ne i} n_j = a_i \pmod {n_i}si​⋅j=i∏​nj​=ai​(modni​)

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}x=⎩⎨⎧​2(mod3)3(mod5)2(mod7)​
35⋅s1≡2⋅s1≡2(mod3)21⋅s2≡s2≡3(mod5)15⋅s3≡s3≡2(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 \\35⋅s1​≡2⋅s1​≡2(mod3)21⋅s2​≡s2​≡3(mod5)15⋅s3​≡s3​≡2(mod7)

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

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

Written by ryan Kim from A41