Prologue
Chinese Remainder Theorem (CRT) is also known as Sun zi’s Theorem. It first appear on the Chinese book called Sūnzǐ Suànjīng, literally The Mathematical Classic of Master Sun/Master Sun’s Mathematical Manual. Here’s the math question in that book.
今有物不知其數,三三數之餘二,五五數之餘三,七七數之餘二,問物幾何?
There is something, but we do not know its exact quantity. When divided by 3, the remainder is 2; when divided by 5, the remainder is 3; when divided by 7, the remainder is 2. What is the quantity?
To solve this, we need to know what the CRT is.
Statement
Suppose n1,n2,…,nk are positive integers which are pairwise coprime (i.e., the greatest common devisor of each pair is 1). For any given integers a1,a2,…,ak, there exists an integer x that satisfies the following system of congruences:
x≡a1(modn1)x≡a2(modn2)⋮x≡ak(modnk)
Moreover, the solution of x is unique modulo N, where
N=n1⋅n2⋅…⋅nk
How it works
- Find N: Compute the product N=n1⋅n2⋅…⋅nk.
- Compute Partial Products: For each i, compute Ni=niN.
- Find Modular Inverse: For each i, find the modular inverse Mi of Ni modulo ni. That is, find Mi such that Ni⋅Mi≡1(modn)i.
- Construct the solution: The solution x can be constructed as:
x=∑i=1kai⋅Ni⋅Mi(modN)
Example
Suppose we want to solve the following system of congruences:
x≡2(mod3)x≡3(mod5)x≡2(mod7)
Compute N=3⋅5⋅7=105.
Compute N1=3105=35,N2=5105=21,N3=7105=15.
Find modular inverses:
35⋅M1≡1(mod3),21⋅M2≡1(mod5),15⋅M3≡1(mod7).
Solving these, we get M1=2,M2=1,M3=1
Construct the solution:
x=[(2⋅35⋅2)+(3⋅21⋅1)+(2⋅15⋅1)](mod105)x=(140+63+30)(mod105)x=233(mod105)x=23(mod105)
So, x=23 is a solution to the system of congruences.