Chinese Remainder Theorem Calculator
Solve x ≡ a (mod m) and x ≡ b (mod n) for coprime whole-number moduli: get the least non-negative solution and the cycle m × n after which answers repeat.
Chinese Remainder Theorem Calculator
Background.
Some time between the third and fifth centuries, a Chinese arithmetic manual, the Sunzi Suanjing, posed a puzzle that has outlived almost everything else in it. There is an unknown number of things. Counted in threes, two are left over; counted in fives, three are left over; counted in sevens, two are left over. How many things are there? The smallest answer is 23, and the general method behind it is now called the Chinese remainder theorem.
Its claim is that remainders on division by moduli sharing no common factor pin a whole number down completely within a cycle as long as the product of those moduli. Remainder 2 on division by 3 and remainder 3 on division by 5 leave exactly one candidate between 0 and 14, the number 8, and every other solution is 8 plus a multiple of 15.
The page solves that two-congruence case, x ≡ a (mod m) and x ≡ b (mod n), returning the least non-negative solution and the combined modulus m × n. Puzzles with three or more conditions, Sunzi's included, are handled by chaining: solve one pair, then pair the answer and its combined modulus with the next condition.
The no-common-factor condition is not a technicality. The traditional Chinese calendar names years by pairing ten heavenly stems with twelve earthly branches, and because 10 and 12 both divide by 2, the pairs repeat after 60 years rather than 120; half of the possible combinations never occur. Moduli like that produce either no solution or one that repeats sooner than m × n, and the page declines them rather than guess.
The same arithmetic runs inside modern cryptography. RSA implementations commonly use it to split one large modular computation into two smaller ones, one for each secret prime, and then recombine the results.
What is chinese remainder theorem calculator?
The Chinese remainder theorem states that when two moduli m and n are coprime, meaning their greatest common divisor is 1, the pair of congruences x ≡ a (mod m) and x ≡ b (mod n) has exactly one solution between 0 and m × n − 1, and every other solution differs from it by a multiple of m × n. The remainders a and b lie between 0 and one less than their moduli. The theorem extends to any number of moduli, provided every pair of them is coprime.
How to use this calculator.
- Enter the first remainder a and its modulus m: for 'leaves 2 when divided by 3', a is 2 and m is 3. Use whole numbers only, and reduce a remainder that is too large first, so 17 mod 5 goes in as 2.
- Enter the second remainder b and modulus n in the same way.
- Make sure the moduli share no factor. 8 and 9 are fine even though neither is prime; 6 and 9 both divide by 3, and the page will refuse them.
- For three or more conditions, solve the first two, then enter that solution as a new remainder and the combined modulus as its modulus alongside the next condition.
- Read the least non-negative solution, and add or subtract the combined modulus to list the others.
The formula.
Every number meeting the first condition can be written x = a + m·k for some whole number k, so the task is to choose k so that the second condition holds as well. That requires m·k ≡ b − a (mod n). Because m and n are coprime, m has an inverse modulo n: a number m⁻¹ with m·m⁻¹ ≡ 1 (mod n), which the page finds with the extended Euclidean algorithm. Multiplying through gives k = (b − a)·m⁻¹ mod n, and then x = (a + m·k) mod (m·n). The final reduction puts the answer between 0 and m·n − 1. If m and n share a factor, m has no inverse modulo n and this construction breaks down, which is why the page checks gcd(m, n) = 1 before anything else. The arithmetic is exact integer arithmetic, and the page refuses moduli whose product reaches 10^15, the point at which it would have to print the answer in rounded scientific notation.
A worked example.
Take the first two clues of Sunzi's puzzle: x ≡ 2 (mod 3) and x ≡ 3 (mod 5). The moduli are coprime, so the combined modulus is 3 × 5 = 15. The inverse of 3 modulo 5 is 2, because 3 × 2 = 6 leaves remainder 1 on division by 5. Then k = (3 − 2) × 2 = 2, and x = 2 + 3 × 2 = 8. As a check, 8 ÷ 3 leaves 2 and 8 ÷ 5 leaves 3, so the least non-negative solution is 8, with the others at 23, 38, 53 and so on. To finish the puzzle, enter 8 with modulus 15 and Sunzi's third clue, 2 with modulus 7. The page returns 23 with combined modulus 105, and 23 does leave 2, 3 and 2 on division by 3, 5 and 7.
Frequently asked questions.
What if my two moduli share a common factor?
Do the moduli have to be prime numbers?
How do I solve three conditions, like Sunzi's original puzzle?
Can I trust the answer when the moduli are very large?
Where is the Chinese remainder theorem used today?
References& sources.
How this page was produced
- Published by
- Quanta Calculator
- Overseen by
- Daniel, Quanta Calculator Researcher
- Primary sources
- 3 cited below
- Method
- x ≡ a (mod m); x ≡ b (mod n), gcd(m,n)=1
- Published
- Last verified
Built with AI assistance and verified by automated tests against the cited sources — every worked example on this page is computed by the same code that runs the calculator. How we build and check calculators.
In this category
Embed
Quanta Pro
Paid features are coming later.
- All 1560 calculators remain free
- No billing is enabled