Audited 31 Jul 2026·Last updated 28 Oct 2026·3 citations·Tier 3·0 uses

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

Least nonnegative solution
8
The smallest whole number, zero or above, that leaves remainder a on division by m and remainder b on division by n.
Combined modulus
15
Model scope
Two-congruence coprime-modulus case with canonical remainders; it does not parse symbolic systems, handle noncoprime compatible systems, or accept noninteger inputs.

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.

  1. 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.
  2. Enter the second remainder b and modulus n in the same way.
  3. 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.
  4. 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.
  5. Read the least non-negative solution, and add or subtract the combined modulus to list the others.

The formula.

x ≡ a (mod m); x ≡ b (mod n), gcd(m,n)=1

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.

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.

remainder A2
remainder B3
modulus M3
modulus N5

Frequently asked questions.

What if my two moduli share a common factor?
Then a solution exists only if a and b leave the same remainder on division by gcd(m, n), and it repeats every lcm(m, n). The page refuses these systems, but you can often rewrite one: x ≡ 3 (mod 6) is the same as x ≡ 1 (mod 2) and x ≡ 0 (mod 3), which may remove the shared factor.
Do the moduli have to be prime numbers?
No, they only have to be coprime to each other. 8 and 9 work perfectly well, since they share no factor even though neither is prime, while 6 and 15 fail because both divide by 3.
How do I solve three conditions, like Sunzi's original puzzle?
Chain the two-condition step. Solve the first pair to get a solution and combined modulus, then treat those as a single new condition and pair it with the third. This works whenever every pair of the original moduli is coprime, as 3, 5 and 7 are.
Can I trust the answer when the moduli are very large?
Only up to a point. Yes, up to a limit. Every step uses exact whole-number arithmetic, so any answer the page shows is exact to the last digit, but it refuses moduli whose product reaches 10^15 rather than print a rounded figure. Beyond that size, use software with arbitrary-precision integers, such as Python or a computer algebra system.
Where is the Chinese remainder theorem used today?
Its best-known modern use is in RSA cryptography, where private-key operations are commonly done separately modulo each of the two secret primes and then recombined, which is considerably faster. It also underlies residue number systems in computer arithmetic and many puzzles about repeating cycles.

How this page was produced

Published by
Quanta Calculator
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
Coming soon