The guide
Three leftovers pin down one number, as long as the counting steps share no factor
A system of remainder clues has exactly one answer below the product of its moduli when they share no factor, and the rule changes in a precise, checkable way when they do.
- 23 mod 105 the answer to Sunzi's puzzle: leftovers 2, 3 and 2 under 3, 5 and 7
- 14 mod 24 6 mod 8 and 2 mod 12 share a factor and still agree
- -9 and 47 the Bezout coefficients of 240 and 46, whose gcd is 2
- 2753 the inverse of 17 modulo 3120
Read the full guide Fold the guide
Someone counts a pile of objects in threes and two are left over. Counted in fives, three are left. Counted in sevens, two are left. How many objects are there? That question, in nearly those words, is the oldest known statement of a problem that still runs inside a great deal of everyday software.
It appears in Sunzi Suanjing, a Chinese mathematical manual written somewhere between the third and fifth centuries of the common era. The date is unsettled, partly because the book was probably put together from older material, and scholars quoted by the MacTutor archive place it anywhere from the third century to roughly the fifth. The book gives the answer, 23, and then a recipe for any leftovers: multiply them by 70, 21 and 15, add, and subtract multiples of 105 while the total is 106 or more.
Those three multipliers are not magic. They are the whole theorem in miniature.
What a congruence says
Two whole numbers are congruent modulo m when their difference is a multiple of m. The clock is the everyday case: four hours after nine o’clock the hand points at one, because 13 and 1 differ by 12. So “x is congruent to 2 modulo 3” only means that x leaves remainder 2 when divided by 3. It is true of 2, 5, 8 and 11, and also of -1, since -1 and 2 differ by 3.
One such line describes a whole family of numbers, spaced m apart and running on forever in both directions. A system of congruences asks which numbers belong to every family at once. Sunzi’s puzzle is three families, spaced 3, 5 and 7 apart, and the question is where they all meet.
They meet at 23, and again every 105 steps.
The history of the method is long. An algorithm for the puzzle was described in India by Aryabhata in the sixth century, and Qin Jiushao’s treatise of 1247 gave a complete solution. In 1801 Gauss introduced the idea of congruences in his book on arithmetic, and the same book works the remainder problem on a calendar question that mixes the solar and lunar cycles with the Roman indiction.
One answer, and what changes when the moduli share a factor
Why is there exactly one answer? Take two numbers that both solve Sunzi’s system. Their difference is a multiple of 3, of 5 and of 7. Because those numbers share no factor, the difference must be a multiple of their product, 105. So no two solutions sit closer than 105 apart, and any stretch of 105 consecutive numbers holds at most one.
Now count. There are 3 times 5 times 7 possible lists of remainders, which is 105, and there are 105 numbers from 0 to 104. Different numbers in that stretch give different lists, so every list is used exactly once. That is the theorem for moduli that pairwise share no factor: one answer below the product, repeating every 105. A residue number system rests on exactly this fact, using lists of remainders as a way to write numbers.
The textbook recipe builds the answer directly. Divide the product by each modulus to get 35, 21 and 15. For each, find the multiple that leaves remainder 1 under its own modulus: 35 times 2 is 70, which leaves 1 when divided by 3, while 21 already leaves 1 under 5 and 15 leaves 1 under 7. These are Sunzi’s 70, 21 and 15. Weight the leftovers, 2 times 70 plus 3 times 21 plus 2 times 15, and the total is 233. Subtract 2 times 105 and the answer is 23.
The recipe collapses the moment two moduli share a factor.
Consider x leaving 6 under 8 and 2 under 12. The moduli share the factor 4, so their product, 96, is the wrong period. The leftovers 6 and 2 differ by 4, a multiple of that shared factor, so the two clues do not contradict each other, and the answer is 14 mod 24. Indeed 14 leaves 6 under 8 and 2 under 12. The period is 24, the least common multiple of 8 and 12, the smallest positive number divisible by both.
Change one leftover and the agreement breaks. Say x leaves 1 under 4 and 2 under 6. The shared factor is 2. The first clue forces x to be odd and the second forces it to be even, and no number is both. In general, a system has a solution exactly when, for every pair of lines, the gcd of the two moduli divides the difference of the two remainders. When it does, the solutions form one class modulo the least common multiple of all the moduli.
Inverses, and the Euclid table behind them
The recipe leans on one operation: finding a number that undoes multiplication. The inverse of 3 modulo 10 is 7, since 3 times 7 is 21, which leaves 1. An inverse exists exactly when the number and the modulus have greatest common divisor 1. The number 4 has none modulo 10, because every multiple of 4 leaves an even remainder under 10, never 1.
The extended Euclidean algorithm finds the inverse, and more. Run on 240 and 46, it returns the gcd, 2, together with the coefficients -9 and 47, and indeed 240 times -9 plus 46 times 47 is 2. That identity, named after Bezout, says that the gcd can always be written as a combination of the two numbers. When the gcd is 1, the coefficient on the first number is its inverse modulo the second.
Take 17 and 3120. The divisions end at 1, so an inverse exists, and it is 2753: 17 times 2753 is 46801, which is 15 times 3120 plus 1. Take a number that shares a factor with its modulus, such as 240 against 46, and the same table ends at 2 instead. Every combination of the two is then even, and 1 is not, so no inverse can exist.
Merging works one line at a time. Keep the class found so far, x modulo L, and bring in the next line, with remainder r and modulus m. The table gives L times p plus m times q equal to g, the gcd. If g divides r minus x, the new modulus is L over g times m, and the new class is x plus L times t, where t is r minus x, over g, times p, taken modulo m over g. For Sunzi’s first two lines this gives 8 mod 15, and the third line takes it to 23 mod 105.
Where splitting a number pays off
The reverse direction matters more to engineers. A calculation on huge numbers can be replaced by several calculations on small ones, one per modulus, and the pieces glued back together with the theorem. The NIST handbook of mathematical functions describes the pattern: a lengthy calculation on 10-digit integers runs on 5-digit residues under four coprime moduli, whose product has 20 digits, twice the digits of the data, and the final result is correct to 20 digits.
Cryptography uses the same trick. Most implementations of RSA apply the theorem when signing and decrypting, and the PKCS #1 standard stores a private key in a form that includes a CRT coefficient, the inverse of one prime modulo the other. The prime-factor fast Fourier transform splits one big transform into two small ones in much the same spirit.
This tool keeps every digit by using JavaScript BigInt values, which stay exact past the largest safe integer, 9007199254740991. BigInt division truncates toward zero, so a remainder can come out negative, and the tool lifts each one back into the range 0 to m minus 1 before it means anything. Long jobs run in a web worker, a background thread, so the page stays responsive and a Cancel button can stop them.
What it cannot do is worth stating. It does not factor large numbers, it does not solve congruences with squares or other powers, and it does not do key work for encryption. It caps input at 200 lines, 2,000 digits per number and ten seconds of work, because merging 200 moduli of 2,000 digits can build a number of several hundred thousand digits.
Try the three systems above. The lines 2 mod 3, 3 mod 5 and 2 mod 7 give 23 mod 105, the lines 6 mod 8 and 2 mod 12 give 14 mod 24, and the lines 1 mod 4 and 2 mod 6 name the two lines that cannot agree.
Sources
Every source consulted for this guide. Links checked November 9, 2026.
- Chinese remainder theorem Wikipedia
- Extended Euclidean algorithm Wikipedia
- Modular multiplicative inverse Wikipedia
- Modular arithmetic Wikipedia
- Least common multiple Wikipedia
- Sunzi Suanjing Wikipedia
- Residue number system Wikipedia
- Sun Zi MacTutor History of Mathematics, University of St Andrews
- DLMF: 27.15 Chinese Remainder Theorem NIST Digital Library of Mathematical Functions
- RFC 8017: PKCS #1: RSA Cryptography Specifications Version 2.2 IETF, RFC Editor
- BigInt MDN Web Docs
- Web Workers API MDN Web Docs