# Chinese remainder theorem

The Chinese remainder theorem is a result about congruences in number theory and its generalizations in abstract algebra. It was first published in the 3rd to 5th centuries by the Chinese mathematician Sun Tzu.

In its basic form, the Chinese remainder theorem will determine a number n that when divided by some given divisors leaves given remainders. For example, what is the lowest number n that when divided by 3 leaves a remainder of 2, when divided by 5 leaves a remainder of 3, and when divided by 7 leaves a remainder of 2?

## Theorem statement

The original form of the theorem, contained in the 5th-century book Sunzi's Mathematical Classic (孫子算經{{#invoke:Category handler|main}}) by the Chinese mathematician Sun Tzu and later generalized with a complete solution called Dayanshu (大衍術{{#invoke:Category handler|main}}) in Qin Jiushao's 1247 Mathematical Treatise in Nine Sections (數書九章{{#invoke:Category handler|main}}, Shushu Jiuzhang), is a statement about simultaneous congruences.

Suppose n1, ..., nk are positive integers that are pairwise coprime. Then, for any given sequence of integers a1, ..., ak, there exists an integer Template:Mvar solving the following system of simultaneous congruences.

${\begin{cases}x\equiv a_{1}&{\pmod {n_{1}}}\\\quad \cdots \\x\equiv a_{k}&{\pmod {n_{k}}}\end{cases}}$ Furthermore, all solutions Template:Mvar of this system are congruent modulo the product, N = n1 ... nk. Hence

$x\equiv y{\pmod {n_{i}}},\quad 1\leq i\leq k\qquad \Longleftrightarrow \qquad x\equiv y{\pmod {N}}.$ Sometimes, the simultaneous congruences can be solved even if the ni are not pairwise coprime. A solution Template:Mvar exists if and only if:

$a_{i}\equiv a_{j}{\pmod {\gcd(n_{i},n_{j})}}\qquad {\text{for all }}i{\text{ and }}j$ All solutions Template:Mvar are then congruent modulo the least common multiple of the ni.

Sun Tzu's work contains neither a proof nor a full algorithm. What amounts to an algorithm for solving this problem was described by Aryabhata (6th century; see Template:Harvnb). Special cases of the Chinese remainder theorem were also known to Brahmagupta (7th century), and appear in Fibonacci's Liber Abaci (1202).

A modern restatement of the theorem in algebraic language is that for a positive integer with prime factorization

$n=p_{1}^{r_{1}}\cdots p_{k}^{r_{k}}$ we have the isomorphism between a ring and the direct product of its prime power parts:

$\mathbf {Z} /n\mathbf {Z} \cong \mathbf {Z} /p_{1}^{r_{1}}\mathbf {Z} \times \cdots \times \mathbf {Z} /p_{k}^{r_{k}}\mathbf {Z}$ The theorem can also be restated in the language of combinatorics as the fact that the infinite arithmetic progressions of integers form a Helly family Template:Harv.

## Existence and uniqueness

The existence and uniqueness of the solution can easily be seen through a non-constructive argument:

There are N = n1 ... nk different Template:Mvar-tuples of remainders. Let us call this set Template:Mvar. On the other hand N = #{1, ..., N}, and each element of {1, ..., N} corresponds to an element of Template:Mvar. Can two numbers a, b ∈ {1, ..., N}, correspond to the same member of Template:Mvar? That is, can they have the same set of remainders when divided by n1, ..., nk? If they did then ab would be divisible by each Template:Mvar. Since the Template:Mvar are relatively prime, ab would be divisible by their product: Template:Mvar. This can't be, so this function {1, ..., N} → R is one-to-one. Since #{1, ..., N} = #R, it must be onto as well. Thus we have established the existence of a bijection.

Existence can be seen by an explicit construction of Template:Mvar. Let [a−1]b denote the multiplicative inverse of a (mod b) given by the Extended Euclidean algorithm. It is defined exactly when Template:Mvar and Template:Mvar are coprime; the following construction explains why this condition is needed.

### Case of two equations (k = 2)

Consider the system:

${\begin{cases}x\equiv a_{1}&{\pmod {n_{1}}}\\x\equiv a_{2}&{\pmod {n_{2}}}\end{cases}}$ Since gcd(n1, n2) = 1, Bézout's identity implies:

$n_{2}\left[n_{2}^{-1}\right]_{n_{1}}+n_{1}\left[n_{1}^{-1}\right]_{n_{2}}=1$ This is true because we are using the inverses provided by the Extended Euclidean algorithm; for any other inverses, this would not necessarily be true, but still be valid (mod n1n2).

Multiplying both sides by Template:Mvar, we get

$x=xn_{2}\left[n_{2}^{-1}\right]_{n_{1}}+xn_{1}\left[n_{1}^{-1}\right]_{n_{2}}$ If we take the congruence modulo n1 for the right-hand-side expression, it is readily seen that

$x\underbrace {n_{2}\left[n_{2}^{-1}\right]_{n_{1}}} _{1}+x\underbrace {n_{1}} _{0}\left[n_{1}^{-1}\right]_{n_{2}}\equiv x\times 1+x\times 0\times \left[n_{1}^{-1}\right]_{n_{2}}\equiv x{\pmod {n_{1}}}$ But we know that xa1 (mod n1), thus this suggests that the coefficient of the first term on the right-hand-side expression can be replaced by a1. Similarly, we can show that the coefficient of the second term can be substituted by a2. We can now define the value

$x\equiv a_{1}n_{2}\left[n_{2}^{-1}\right]_{n_{1}}+a_{2}n_{1}\left[n_{1}^{-1}\right]_{n_{2}}$ and it is seen to satisfy both congruences, for example:

$a_{1}n_{2}\left[n_{2}^{-1}\right]_{n_{1}}+a_{2}n_{1}\left[n_{1}^{-1}\right]_{n_{2}}\equiv a_{1}\times 1+a_{2}\times 0\times \left[n_{1}^{-1}\right]_{n_{2}}\equiv a_{1}{\pmod {n_{1}}}$ ### General case

The same type of construction works in the general case of Template:Mvar congruence equations. Let N = n1 ... nk be the product of every modulus then define

$x:=\left[\sum _{i}a_{i}{\frac {N}{n_{i}}}\left[\left({\frac {N}{n_{i}}}\right)^{-1}\right]_{n_{i}}\right]_{N}$ and this is seen to satisfy the system of congruences by a similar calculation as before.

## Finding the solution with basic algebra and modular arithmetic

For example, consider the problem of finding an integer Template:Mvar such that

${\begin{cases}x\equiv 2{\pmod {3}}\\x\equiv 3{\pmod {4}}\\x\equiv 1{\pmod {5}}\end{cases}}$ ### Brute-force approach

A brute-force approach converts these congruences into sets and writes the elements out to the product of 3×4×5 = 60 (the solutions modulo 60 for each congruence):

x ∈ {2, 5, 8, 11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 56, 59, 62, 65, 68, 71, 74, ...}
x ∈ {3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 71, 75, 79, ...}
x ∈ {1, 6, 11, 16, 21, 26, 31, 36, 41, 46, 51, 56, 61, 66, 71, 76, 81, 86, 91, 96, ...}

To find an x that satisfies all three congruences, intersect the three sets to get:

x ∈ {11, 71, ...}

Which can be expressed as

$x\equiv 11{\pmod {60}}$ ### Algebraic approach

Another way to find a solution is with basic algebra, modular arithmetic, and stepwise substitution.

We start by translating these congruences into equations for some Template:Mvar, and Template:Mvar:

${\begin{cases}x=2+3t\\x=3+4s\\x=1+5u\end{cases}}$ Start by substituting the Template:Mvar from the first equation into the second congruence:

{\begin{aligned}2+3t&\equiv 3&&{\pmod {4}}\\3t&\equiv 1&&{\pmod {4}}\\t&\equiv (3)^{-1}\equiv 3&&{\pmod {4}}\end{aligned}} meaning that t = 3 + 4s for some integer Template:Mvar. Substitute Template:Mvar into the first equation:

$x=2+3t=2+3(3+4s)=11+12s$ Substitute this Template:Mvar into the third congruence:

{\begin{aligned}11+12s&\equiv 1&&{\pmod {5}}\\1+2s&\equiv 1&&{\pmod {5}}\\2s&\equiv 0&&{\pmod {5}}\end{aligned}} meaning that s = 0 + 5u for some integer Template:Mvar. Finally,

$x=11+12s=11+12(5u)=11+60u$ So, we have solutions {11, 71, 131, 191, ...}.

Notice that 60 = lcm(3,4,5). If the moduli are pairwise coprime (as they are in this example), the solutions will be congruent modulo their product.

## A constructive algorithm to find the solution

The following algorithm only applies if the ni are pairwise coprime. (For simultaneous congruences when the moduli are not pairwise coprime, the method of successive substitution can often yield solutions.)

Suppose, as above, that a solution is required for the system of congruences:

$x\equiv a_{i}{\pmod {n_{i}}},\qquad i=1,\cdots ,k.$ Define: N = n1 ... nk. For each Template:Mvar, the integers ni and {{ safesubst:#invoke:Unsubst||$B=N/ni}} are coprime. Using the extended Euclidean algorithm we can find ri, siZ such that rini + {{ safesubst:#invoke:Unsubst||$B=siN/ni}} = 1. Substitute ei for {{ safesubst:#invoke:Unsubst||$B=siN/ni}}, to arrive at: rini + ei = 1. So the remainder of ei divided by ni is 1. On the other hand, ei = {{ safesubst:#invoke:Unsubst||$B=siN/ni}}, guarantees that nj divides ei for ji. To summarize:

$e_{i}\equiv {\begin{cases}1{\pmod {n_{i}}}\\0{\pmod {n_{j}}}&j\neq i\end{cases}}$ Because of this, and the multiplication rules allowed in congruences, one solution to the system of simultaneous congruences is:

$x=\sum _{i=1}^{k}a_{i}e_{i}$ For example, consider the problem of finding an integer Template:Mvar such that

${\begin{cases}x\equiv 2&{\pmod {3}}\\x\equiv 3&{\pmod {4}}\\x\equiv 1&{\pmod {5}}\end{cases}}$ Using the extended Euclidean algorithm, for Template:Mvar modulo 3 and 20 [4 × 5], we find (−13) × 3 + 2 × 20 = 1; i.e., e1 = 40. For Template:Mvar modulo 4 and 15 [3 × 5], we get (−11) × 4 + 3 × 15 = 1, i.e. e2 = 45. Finally, for Template:Mvar modulo 5 and 12 [3 × 4], we get 5 × 5 + (−2) × 12 = 1, i.e. e3 = −24. A solution Template:Mvar is therefore 2 × 40 + 3 × 45 + 1 × (−24) = 191. All other solutions are congruent to 191 modulo 60, [3 × 4 × 5], which means they are all congruent to 11 modulo 60.

Note: There are multiple implementations of the extended Euclidean algorithm which will yield different sets of e1 = −20, e2 = −15, and e3 = −24. These sets however will produce the same solution; i.e., (−20)2 + (−15)3 + (−24)1 = −109 ≡ 11 (mod 60).

## Statement for principal ideal domains

Chinese Remainder Theorem for Principal Ideal Domains. Let Template:Mvar be principal ideal domain. If u1, ..., uk are pairwise coprime elements of Template:Mvar where u = u1...uk, then the quotient ring R/uR and the product ring R/u1R × ... × R/ukR are isomorphic via the following map:
${\begin{cases}f:R/uR\to R/u_{1}R\times \cdots \times R/u_{k}R\\f(x+uR)=(x+u_{1}R,\ldots ,x+u_{k}R)\end{cases}}$ This statement is a straightforward generalization of the above theorem about integer congruences: Z is a principal ideal domain, the surjectivity of the map f shows that every system of congruences of the form

$x\equiv a_{i}{\pmod {u_{i}}}\qquad 1\leq i\leq k$ can be solved for Template:Mvar, and the injectivity of the map f shows that all the solutions Template:Mvar are congruent modulo Template:Mvar.

Proof. This map is well-defined and a homomorphism of rings. An inverse homomorphism can be constructed as follows, showing that it is in fact an isomorphism. For each Template:Mvar, the elements ui and u/ui are coprime, and therefore there exist elements Template:Mvar and Template:Mvar in Template:Mvar with

$ru_{i}+su/u_{i}=1$ Set ei = su/ui. Then it is clear that

$e_{i}\equiv \delta _{ij}{\pmod {u_{j}R}}.$ Thus the inverse of f is the map

${\begin{cases}g:R/u_{1}R\times \cdots \times R/u_{k}R\to R/uR\\g(a_{1}+u_{1}R,\ldots ,a_{k}+u_{k}R)=\sum _{i=1}^{k}a_{i}e_{i}+uR\end{cases}}$ ## Statement for general rings

The general form of the Chinese remainder theorem, which implies all the statements given above, can be formulated for commutative rings and ideals.

Chinese Remainder Theorem for Commutative Rings. If Template:Mvar is a commutative ring and I1, ..., Ik are ideals of R that are pairwise coprime (meaning Ii + Ij = R for all ij), then the product Template:Mvar of these ideals is equal to their intersection, and the quotient ring R/I is isomorphic to the product ring R/I1 × ... × R/Ik via the isomorphism
${\begin{cases}f:R/I\to R/I_{1}\times \cdots \times R/I_{k}\\f(x+I)=(x+I_{1},\cdots ,x+I_{k})\end{cases}}$ Here is a version of the theorem where R is not required to be commutative:

Chinese Remainder Theorem for Noncommutative Rings. Let Template:Mvar be any ring with 1 (not necessarily commutative) and I1, ..., Ik be pairwise coprime 2-sided ideals. Then the canonical Template:Mvar-module homomorphism RR/I1 × ... × R/Ik is onto, with kernel I1 ∩ ... ∩ Ik. Hence, $R/(I_{1}\,\cap \,\cdots \,\cap \,I_{k})\,\simeq \,R/I_{1}\,\times \,\cdots \,\times \,R/I_{k}$ (as Template:Mvar-modules).

## Applications

### Sequence Numbering

The Chinese remainder theorem can be used to construct an elegant Gödel numbering for sequences, which is needed to prove Gödel's incompleteness theorems.

### Fast Fourier Transform

The Good-Thomas fast Fourier transform algorithm exploits a re-indexing of the data based on the Chinese remainder theorem. The Prime-factor FFT algorithm contains an implementation.

### Encryption

The Chinese remainder theorem can also be used in secret sharing, which consists of distributing a set of shares among a group of people who, all together (but no one alone), can recover a certain secret from the given set of shares. Each of the shares is represented in a congruence, and the solution of the system of congruences using the Chinese remainder theorem is the secret to be recovered. Secret Sharing using the Chinese Remainder Theorem uses, along with the Chinese remainder theorem, special sequences of integers that guarantee the impossibility of recovering the secret from a set of shares with less than a certain cardinality.

### Hermite Interpolation

The General Hermite Interpolation Problem. Given Template:Mvar complex points ("interpolation nodes") λ1, ..., λr and complex data {aj,k : 1 ≤ jr, 0 ≤ k < νj}, find P(x) ∈ C[x] such that:
$P^{(k)}(\lambda _{j})=a_{j,k}\qquad 1\leq j\leq r,\quad 0\leq k<\nu _{j}.$ Solution. Introducing the polynomials

$A_{j}(x):=\sum _{k=0}^{\nu _{j}-1}{\frac {a_{j,k}}{k!}}(x-\lambda _{j})^{k}$ the problem may be equivalently reformulated as a system of Template:Mvar simultaneous congruences:

$P(x)\equiv A_{j}(x){\pmod {(x-\lambda _{j})^{\nu _{j}}}},\qquad 1\leq j\leq r$ By the Chinese remainder theorem in the principal ideal domain C[x], there is a unique polynomial P(x) such that:

$\deg(P) A direct construction, in analogy with the above proof for the integer number case, can be performed as follows. Define the polynomials

{\begin{aligned}Q&=\prod _{i=1}^{r}(x-\lambda _{i})^{\nu _{i}}\\Q_{j}&={\frac {Q}{(x-\lambda _{j})^{\nu _{j}}}}\end{aligned}} The partial fraction decomposition of {{ safesubst:#invoke:Unsubst||$B=1/Q}} gives Template:Mvar polynomials Sj with degrees deg(Sj) < νj such that ${\frac {1}{Q}}=\sum _{i=1}^{r}{\frac {S_{i}}{(x-\lambda _{i})^{\nu _{i}}}}$ so that $1=\sum _{i=1}^{r}S_{i}Q_{i}.$ Then a solution of the simultaneous congruence system is given by the polynomial $\sum _{i=1}^{r}A_{i}S_{i}Q_{i}=A_{j}+\sum _{i=1}^{r}(A_{i}-A_{j})S_{i}Q_{i}\equiv A_{j}{\pmod {(x-\lambda _{j})^{\nu _{j}}}}\qquad 1\leq j\leq r$ and the minimal degree solution is this one reduced modulo Template:Mvar, that is the unique with degree less than Template:Mvar. ### Dedekind's Theorem Dedekind's Theorem on the Linear Independence of Characters. Let Template:Mvar be a monoid and Template:Mvar an integral domain, viewed as a monoid by considering the multiplication on Template:Mvar. Then any finite family fi )iI of distinct monoid homomorphisms fi : Mk is linearly independent. In other words every family (αi)iI of elements αik satisfying $\sum _{i\in I}\alpha _{i}f_{i}=0$ must be equal to the family (0)iI. Proof. First assume that Template:Mvar is a field, otherwise, replace the integral domain Template:Mvar by its quotient field, and nothing will change. We can linearly extend the monoid homomorphisms fi : Mk to Template:Mvar-algebra homomorphisms Fi : k[M] → k, where k[M] is the monoid ring of Template:Mvar over Template:Mvar. Then, by linearity, the condition $\sum _{i\in I}\alpha _{i}f_{i}=0,$ yields $\sum _{i\in I}\alpha _{i}F_{i}=0.$ Next, for i, jI; ij the two Template:Mvar-linear maps Fi : k[M] → k and Fj : k[M] → k are not proportional to each other. Otherwise fi and fj would also be proportional, and thus equal since as monoid homomorphisms they satisfy: fi (1) = 1 = fj (1), which contradicts the assumption that they are distinct. Therefore the kernels Ker Fi and Ker Fj are distinct. Since k[M]/Ker FiFi(k[M]) = k is a field, Ker Fi is a maximal ideal of k[M] for every iI. Because they are distinct and maximal the ideals Ker Fi and Ker Fj are coprime whenever ij. The Chinese Remainder Theorem (for general rings) yields an isomorphism: ${\begin{cases}\phi :k[M]/K\to \prod _{i\in I}k[M]/\mathrm {Ker} F_{i}\\\phi (x+K)=\left(x+\mathrm {Ker} F_{i}\right)_{i\in I}\end{cases}}$ where $K=\prod _{i\in I}\mathrm {Ker} F_{i}=\bigcap _{i\in I}\mathrm {Ker} F_{i}.$ Consequently, the map ${\begin{cases}\Phi :k[M]\to \prod _{i\in I}k[M]/\mathrm {Ker} F_{i}\\\Phi (x)=\left(x+\mathrm {Ker} F_{i}\right)_{i\in I}\end{cases}}$ is surjective. Under the isomorphisms k[M]/Ker FiFi(k[M]) = k, the map Φ corresponds to: ${\begin{cases}\psi :k[M]\to \prod _{i\in I}k\\\psi (x)=\left[F_{i}(x)\right]_{i\in I}\end{cases}}$ Now, $\sum _{i\in I}\alpha _{i}F_{i}=0$ yields $\sum _{i\in I}\alpha _{i}u_{i}=0$ for every vector (ui)iI in the image of the map Template:Mvar. Since Template:Mvar is surjective, this means that $\sum _{i\in I}\alpha _{i}u_{i}=0$ for every vector $\left(u_{i}\right)_{i\in I}\in \prod _{i\in I}k.$ Consequently, (αi)iI = (0)iI. QED. ## Non-commutative case: a caveat Sometimes in the commutative case, the conclusion of the Chinese Remainder Theorem is stated as R/(I1 ... Ik) ≅ R/I1 × ... × R/Ik. This version does not hold in the non-commutative case, since I1 ∩ ... ∩ IkI1 ... Ik, as can be seen from the following case: Proposition. Let Template:Mvar be the ring of non-commutative real polynomials in Template:Mvar and Template:Mvar. Let Template:Mvar be the principal two-sided ideal generated by Template:Mvar and Template:Mvar the principal two-sided ideal generated by xy + 1. Then I + J = R but IJIJ. Proof. Observe that Template:Mvar is formed by all polynomials with an Template:Mvar in every term and that every polynomial in Template:Mvar vanishes under the substitution y = − {{ safesubst:#invoke:Unsubst||$B=1/x}}. Then clearly p = (xy + 1)xIJ. Define a "term in Template:Mvar", as an element of the multiplicative monoid of Template:Mvar generated by Template:Mvar and Template:Mvar, and its degree as the usual degree of the term after the substitution y = x. On the other hand, suppose qJ. Observe that a term in Template:Mvar of maximum degree depends on Template:Mvar otherwise Template:Mvar under the substitution y = − {{ safesubst:#invoke:Unsubst||\$B=1/x}} can not vanish. The same happens then for an element qIJ. Note that the last Template:Mvar, from left to right, in a term of maximum degree in an element of Template:Mvar is preceded by more than one Template:Mvar. (We are counting here all the preceding Template:Mvars. E.g., in x2yxyx5 the last Template:Mvar is preceded by three Template:Mvars.) This proves that p = (xy + 1)xIJ since the last Template:Mvar in the term of maximum degree in Template:Mvar (Template:Mvar) is preceded by only one Template:Mvar. Hence IJIJ.

However, it is true in general that I + J = R implies IJ = IJ + JI. To see this, note that IJ = (IJ)(I + J) ⊂ IJ + JI, while the opposite inclusion is obvious. Also, we have in general that, provided I1, ..., Im are pairwise coprime two-sided ideals in Template:Mvar, the natural map

$R/(I_{1}\cap \cdots \cap I_{m})\to R/I_{1}\oplus \cdots \oplus R/I_{m}$ is an isomorphism. Note that I1 ∩ ... ∩ Im can be replaced by a sum over all orderings of I1, ..., Im of their product (or just a sum over enough orderings, using inductively that IJ = IJ + JI for coprime ideals I, JR).