# .

# 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, which is contained in the 5th-century book Sunzi's Mathematical Classic (孫子算經) by the Chinese mathematician Sun Tzu and later generalized with a complete solution called Dayanshu (大衍術) in Qin Jiushao's 1247 Mathematical Treatise in Nine Sections (數書九章, 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 x 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 x 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 x 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 x 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 Kak 1986). 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 (Duchet 1995).

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 k-tuples of remainders. Let us call this set R. On the other hand N = #{1, ..., N}, and each element of {1, ..., N} corresponds to an element of R. Can two numbers a, b ∈ {1, ..., N}, correspond to the same member of R? That is, can they have the same set of remainders when divided by n1, ..., nk? If they did then a − b would be divisible by each ni. Since the ni are relatively prime, a − b would be divisible by their product: N. 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 x. Let [a−1]b denote the multiplicative inverse of a (mod b) given by the Extended Euclidean algorithm. It is defined exactly when a and b 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 x, we get

\( x = x n_2 \left [n_2^{-1} \right ]_{n_1} + x n_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 x ≡ a1 (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 k 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 x 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 t, s, and u:

\( \begin{cases}x = 2 + 3t \\ x = 3 + 4s \\ x = 1 + 5u \end{cases} \)

Start by substituting the x from the first equation into the second congruence:

\( \begin{align} 2 + 3t &\equiv 3 &&\pmod{4} \\ 3t &\equiv 1 && \pmod{4} \\ t &\equiv (3)^{-1} \equiv 3 &&\pmod{4} \end{align} \)

meaning that t = 3 + 4s for some integer s. Substitute t into the first equation:

x = 2 + 3t = 2 + 3(3 + 4s) = 11 + 12s

Substitute this x into the third congruence:

\( \begin{align} 11 + 12s &\equiv 1 && \pmod{5} \\ 1 + 2s &\equiv 1 &&\pmod{5} \\ 2s &\equiv 0 &&\pmod{5} \end{align} \)

meaning that s = 0 + 5u for some integer u. 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 i, the integers ni and N/ni are coprime. Using the extended Euclidean algorithm we can find ri, si ∈ Z such that rini + siN/ni = 1. Substitute ei for siN/ni, to arrive at: rini + ei = 1. So the remainder of ei divided by ni is 1. On the other hand, ei = siN/ni, guarantees that nj divides ei for j ≠ i. To summarize:

\( e_i \equiv \begin{cases} 1 \pmod{n_i} \\ 0 \pmod{n_j} & j \ne 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 x 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 x modulo 3 and 20 [4 × 5], we find (−13) × 3 + 2 × 20 = 1; i.e., e1 = 40. For x modulo 4 and 15 [3 × 5], we get (−11) × 4 + 3 × 15 = 1, i.e. e2 = 45. Finally, for x modulo 5 and 12 [3 × 4], we get 5 × 5 + (−2) × 12 = 1, i.e. e3 = −24. A solution x 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.

Or in words:

Find a number ei that gives modulo 1 for a particular divisor, but is divisible by all other divisors (coprime, in this case).

Multiply the remainder (of the original number N to this particular divisor). Repeat with all the divisors.

Sum all these products together.

Divide the sum by the LCM of all the divisors. N shares the modulo with the final remainder.

Thus, we have ei values of 40 (divisible by 4 and 5; gives 1 for mod 3), 45 (for mod 4) and 36 (for mod 5). Multiplying by the given remainders 2, 3 and 1 yields 80, 135 and 36 respectively. The sum, 251, is then divided by 60, yielding remainder 11.

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).

Using the same principle, the answer to Sun Tzu's original question is therefore 23, using ei values of 70, 21 and 15 for divisors 3, 5, 7: (70)2 + (21)3 + (15)2 = 233 ≡ 23 (mod 105).

Statement for principal ideal domains

Chinese Remainder Theorem for Principal Ideal Domains. Let R be principal ideal domain. If u1, ..., uk are pairwise coprime elements of R 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_1R \times \cdots \times R/u_k R \\ f(x +uR) = (x + u_1R, \ldots, x + u_kR) \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 x, and the injectivity of the map f shows that all the solutions x are congruent modulo u.

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 i, the elements ui and u/ui are coprime, and therefore there exist elements r and s in R with

\( r u_i + s u/u_i = 1 \)

Set ei = su/ui. Then it is clear that

\( e_i \equiv \delta_{ij} \pmod{u_jR}. \)

Thus the inverse of f is the map

\( \begin{cases}g: R/u_1R \times \cdots \times R/u_kR \to R/uR \\ g(a_1 + u_1R, \ldots, a_k + u_kR) = \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 R is a commutative ring and I1, ..., Ik are ideals of R that are pairwise coprime (meaning Ii + Ij = R for all i ≠ j), then the product I 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 R be any ring with 1 (not necessarily commutative) and I1, ..., Ik be pairwise coprime 2-sided ideals. Then the canonical R-module homomorphism R → R/I1 × ... × R/Ik is onto, with kernel I1 ∩ ... ∩ Ik. Hence, \( \scriptstyle R/(I_1 \,\cap\, \cdots \,\cap\, I_k) \,\simeq\, R/I_1 \,\times\, \cdots \,\times\, R/I_k \) (as R-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 r complex points ("interpolation nodes") λ1, ..., λr and complex data {aj,k : 1 ≤ j ≤ r, 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 r 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) < n := \sum_j\nu_j. \)

A direct construction, in analogy with the above proof for the integer number case, can be performed as follows. Define the polynomials

\( \begin{align} Q &= \prod_{i=1}^{r}(x - \lambda_i)^{\nu_i} \\ Q_j &= \frac{Q}{(x - \lambda_j)^{\nu_j}} \end{align} \)

The partial fraction decomposition of 1/Q gives r 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 Q, that is the unique with degree less than n.

Dedekind's Theorem

Dedekind's Theorem on the Linear Independence of Characters. Let M be a monoid and k an integral domain, viewed as a monoid by considering the multiplication on k. Then any finite family ( fi )i∈I of distinct monoid homomorphisms fi : M → k is linearly independent. In other words every family (αi)i∈I of elements αi ∈ k satisfying

\( \sum_{i \in I}\alpha_i f_i = 0 \)

must be equal to the family (0)i∈I.

Proof. First assume that k is a field, otherwise, replace the integral domain k by its quotient field, and nothing will change. We can linearly extend the monoid homomorphisms fi : M → k to k-algebra homomorphisms Fi : k[M] → k, where k[M] is the monoid ring of M over k. 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, j ∈ I; i ≠ j the two k-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 Fi ≅ Fi(k[M]) = k is a field, Ker Fi is a maximal ideal of k[M] for every i ∈ I. Because they are distinct and maximal the ideals Ker Fi and Ker Fj are coprime whenever i ≠ j. 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 Fi → Fi(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)i∈I in the image of the map ψ. Since ψ 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)i∈I = (0)i∈I. 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 ∩ ... ∩ Ik ≠ I1 ... Ik, as can be seen from the following case:

Proposition. Let R be the ring of non-commutative real polynomials in x and y. Let I be the principal two-sided ideal generated by x and J the principal two-sided ideal generated by xy + 1. Then I + J = R but I ∩ J ≠ IJ.

Proof. Observe that I is formed by all polynomials with an x in every term and that every polynomial in J vanishes under the substitution y = − 1/x. Then clearly p = (xy + 1)x ∈ I ∩ J. Define a "term in R", as an element of the multiplicative monoid of R generated by x and y, and its degree as the usual degree of the term after the substitution y = x. On the other hand, suppose q ∈ J. Observe that a term in q of maximum degree depends on y otherwise q under the substitution y = − 1/x can not vanish. The same happens then for an element q ∈ IJ. Note that the last y, from left to right, in a term of maximum degree in an element of IJ is preceded by more than one x. (We are counting here all the preceding xs. E.g., in x2yxyx5 the last y is preceded by three xs.) This proves that p = (xy + 1)x ∉ IJ since the last y in the term of maximum degree in p (xyx) is preceded by only one x. Hence I ∩ J ≠ IJ.

However, it is true in general that I + J = R implies I ∩ J = IJ + JI. To see this, note that I ∩ J = (I ∩ J)(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 R, 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 I ∩ J = IJ + JI for coprime ideals I, J ⊂ R).

See also

Covering system

Hasse principle

Residue number system

Secret sharing using the Chinese remainder theorem

Notes

Gauss & Clarke (1986, Art. 32-36)

References

This article includes a list of references, related reading or external links, but its sources remain unclear because it lacks inline citations. Please improve this article by introducing more precise citations. (February 2015)

Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L.; Stein, Clifford (2001), Introduction to Algorithms (Second ed.), MIT Press and McGraw-Hill, ISBN 0-262-03293-7. See Section 31.5: The Chinese remainder theorem, pp. 873–876.

Ding, Cunsheng; Pei, Dingyi; Salomaa, Arto (1996), Chinese Remainder Theorem: Applications in Computing, Coding, Cryptography, World Scientific Publishing, pp. 1–213, ISBN 981-02-2827-9

Duchet, Pierre (1995), "Hypergraphs", in Graham, R. L.; Grötschel, M.; Lovász, L., Handbook of combinatorics, Vol. 1, 2, Amsterdam: Elsevier, pp. 381–432, MR 1373663. See in particular Section 2.5, "Helly Property", pp. 393–394.

Gauss, Carl Friedrich; Clarke, Arthur A. (translator into English) (1986), Disquisitiones Arithemeticae (Second, corrected ed.), New York: Springer, ISBN 978-0-387-96254-2

Hungerford, Thomas W. (1974), Algebra, Graduate Texts in Mathematics, Vol. 73, Springer-Verlag, pp. 131–132, ISBN 978-1-4612-6101-8

Kak, Subhash (1986), "Computational aspects of the Aryabhata algorithm" (PDF), Indian Journal of History of Science 21 (1): 62–71

Knuth, Donald (1997), The Art of Computer Programming, Volume 2: Seminumerical Algorithms (Third ed.), Addison-Wesley, ISBN 0-201-89684-2. See Section 4.3.2 (pp. 286–291), exercise 4.6.2–3 (page 456).

Leonardo Pisano; Sigler, Laurence E. (translator into English) (2002), Fibonacci's Liber Abaci, Springer-Verlag, pp. 402–403, ISBN 0-387-95419-8

External links

Hazewinkel, Michiel, ed. (2001), "Chinese remainder theorem", Encyclopedia of Mathematics, Springer, ISBN 978-1-55608-010-4

"Chinese Remainder Theorem" by Ed Pegg, Jr., Wolfram Demonstrations Project, 2007.

Weisstein, Eric W., "Chinese Remainder Theorem", MathWorld.

C# program and discussion at codeproject

University of Hawaii System CRT by Lee Lady

Full text of the Sunzi Suanjing (Chinese) — Chinese Text Project

Retrieved from "http://en.wikipedia.org/"

All text is available under the terms of the GNU Free Documentation License