logo

Proof of the Chinese Remainder Theorem 📂Number Theory

Proof of the Chinese Remainder Theorem

Theorem

gcd(n,m)=1\gcd(n,m) = 1 and {xb(modn)xc(modm)\begin{cases} x \equiv b \pmod{n} \\ x \equiv c \pmod{m} \end{cases} have exactly one solution in 1xnm1 \le x \le nm.

Explanation

It is said that a mathematics book written in China between the 3rd and 5th centuries AD contained this problem.

If a number is grouped by threes, two remain; by fives, three remain; and by sevens, two remain. What is the number? - Sunzi’s Arithmetic, Exercise 26

Using the modern mathematical expression, it becomes a problem of finding the solution to {x2(mod3)x3(mod5)x2(mod7) \begin{cases} x \equiv 2 \pmod{3} \\ x \equiv 3 \pmod{5} \\ x \equiv 2 \pmod{7} \end{cases} . The theorem used today to solve this problem is known as the “Chinese remainder theorem.” It is because the earliest recorded solution to simultaneous congruence equations, regardless of time and place, was introduced in a Chinese mathematics book.

Note that the author of Sunzi’s Arithmetic is known only by the surname Sun and has no relation to Sun Wu, who wrote “The Art of War.”

As seen in the example, even if more than one equation is given, it does not matter at all.

Proof

Strategy: Concrete proof of a unique solution.


xb(modn) \begin{equation} x \equiv b \pmod{n} \end{equation} xc(modm) \begin{equation} x \equiv c \pmod{m} \end{equation} since xb(modn)x \equiv b \pmod{n}, there exists yZy \in \mathbb{Z} satisfying x=ny+bx = ny + b, and substituting this into (2)(2) gives nycb(modm) ny \equiv c - b \pmod{m} . Meanwhile, the congruence above has a unique solution y0y_{0} in 1ym 1 \le y \le m, and therefore, x=ny0+bx = ny_{0} + b has a unique solution x0x_{0} in 1xnm 1 \le x \le nm. Hence, x0=ny0+b x_{0} = ny_{0} + b . Here, x0x_{0} is the solution to (1)(1). Now substituting ny0=x0bny_{0} = x_{0} - b into ny0cb(modm)ny_{0} \equiv c - b \pmod{m} gives x0c(modm) x_{0} \equiv c \pmod{m} . In other words, x0x_{0} is confirmed to be the solution to (2)(2).

Code

The following code implements the Chinese remainder theorem in R language. It finds the solution when given a matrix of size n×2n \times 2. For the given problem {x2(mod3)x3(mod5)x2(mod7)\begin{cases} x \equiv 2 \pmod{3} \\ x \equiv 3 \pmod{5} \\ x \equiv 2 \pmod{7} \end{cases}, just input the matrix S:=[233527]S := \begin{bmatrix} 2 & 3 \\ 3 & 5 \\ 2 & 7 \end{bmatrix}.

CRA<-function(S)  #Algorithm of chinese remainder theorem
{
  r<-S[,1]      # matrix S express below sysyem.
  mod<-S[,2]    # x = r[1] (mod mod[1])
  n<-length(r)  # x = r[2] (mod mod[2])
  # x = r[3] (mod mod[3])
  
  A<-seq(r[1],to=mod[1]*mod[2],by=mod[1])
  
  for(i in 2:n)
  {
    B=seq(r[i],to=mod[1]*mod[i],by=mod[i])
    r[1]=min(A[A %in% B])
    mod[1]=mod[1]*mod[i]
    if (i<n) {A=seq(r[1],to=mod[1]*mod[i+1],by=mod[1])}
  }
  
  return(r[1])
}
 
example<-matrix(c(2,3,3,5,2,7),ncol=2,byrow=T); example
CRA(example)

The result of executing the above code is as follows. Let’s confirm that x=23x=23 is the answer to the given problem.

20190227_212607.png