Chinese Remainder Theorem Calculator - Solve Congruences

The system to solve — one congruence per row

3 of 8 rows used

Sun Tzu's soldiersThe original problem from the Sunzi Suanjing, written some time between the 3rd and 5th century: counted in threes there are 2 left over, in fives 3 left over, in sevens 2 left over.

Solution to all 3 congruences

x ≡ 23 (mod 105)

Smallest solution

23

the only one below 105

Combined modulus

105

lcm of every modulus — the repeat period

Product of moduli

105

equals the lcm — the moduli are pairwise coprime

Work done

2 merges

instead of testing up to 105 candidates

Every solution

x = 23 + 105k

23, 128, 233, 338, 443, …

Negative k works too, so -82 and -187 satisfy the system as well. One residue class, not one number.

Puzzle questions usually ask for the smallest count over some bound

Merging one congruence at a time — each row folds the next congruence into the running answer

StepMerging ingcd(M, n)Gap a − xRunning xRunning modulus M
0start: x ≡ 2 (mod 3)23
1x ≡ 3 (mod 5)11815
2x ≡ 2 (mod 7)1-623105

A step succeeds when gcd(M, n) divides the gap. Coprime moduli make that gcd 1, which divides everything — that is the entire reason the classical theorem asks for coprimality.

Checking 23 against every congruence

Congruencex mod nᵢRequiredMatch
x ≡ 2 (mod 3)22
x ≡ 3 (mod 5)33
x ≡ 2 (mod 7)22

Every integer from 0 to 104, one strip per congruence

A cell is filled where that number satisfies that congruence. Exactly one column is filled in every strip — column 23, marked in amber.

2 (mod 3)
3 (mod 5)
2 (mod 7)

The strips have periods 3, 5, 7, so the pattern of all of them together can only repeat after 105 steps — which is why there is exactly one answer in that window and never two.

How to Use This Calculator

  1. Type each congruence into its own row: the remainder aᵢ on the left, the modulus nᵢ on the right. A remainder larger than its modulus, or a negative one, gets reduced automatically and the note under the field shows the reduction.
  2. Use Add congruence for systems with more than two conditions — up to eight rows, and the × button drops a row you no longer need.
  3. Read the result card: it gives the answer as a residue class, x ≡ a (mod M), because every number in that class solves the system.
  4. Switch Method to trace between the sequential merge and the Gauss construction to see the same answer built two different ways. The merge handles moduli that share factors; the construction shows the M / Mᵢ / yᵢ table used in textbooks.
  5. For puzzle questions like “the smallest number over 1,000”, put the bound in the smallest solution at or above field.

Share this calculator

Help others solve their calculations

Found this calculator helpful? Share it with your friends, students, or colleagues who might need it!

Chinese Remainder Theorem Calculator: One Number From Many Remainders

About the Author

Marko Šinko - Co-Founder & Lead Developer

Marko Šinko

Co-Founder & Lead Developer, AI Math Calculator

Lepoglava, Croatia
Advanced Algorithm Expert

Croatian developer with a Computer Science degree from University of Zagreb and expertise in advanced algorithms. Co-founder of award-winning projects, ensuring precise mathematical computations and reliable calculator tools.

📅 Published:
Chinese Remainder Theorem Calculator showing three modular dials with different remainders merging into one dial marking the solution

A Chinese Remainder Theorem calculator settles a riddle that has been in circulation for about seventeen centuries. Count a pile of objects in threes and 2 are left over; count the same pile in fives and 3 are left over; count it in sevens and 2 are left over. How many objects? The answer is 23, and the remarkable part isn’t that an answer exists — it’s that those three scraps of leftover information pin down the pile completely, to one number in every 105.

That is the whole theorem in one sentence, and it turns out to be load-bearing. The same result is what lets a server decrypt an RSA message four times faster than the naive route, and what lets competitive programmers reconstruct a giant number from a handful of small ones without ever storing the giant number.

An Army Counted in Threes, Fives and Sevens

The problem appears in the Sunzi Suanjing, a Chinese arithmetic manual written somewhere between the 3rd and 5th century. The text poses it, gives the answer 23, and moves on without much justification — the usual story is that a general counted his troops by having them form rows, since counting the leftovers in a formation of thousands is much faster than counting the men. Whatever the origin, the mathematics is exact:

x ≡ 2 (mod 3), x ≡ 3 (mod 5), x ≡ 2 (mod 7) → x ≡ 23 (mod 105)

Try 23 against each condition: 23 = 7×3 + 2, so remainder 2 modulo 3. 23 = 4×5 + 3, remainder 3. 23 = 3×7 + 2, remainder 2. All three hold. But 23 is not the only answer, and the mistake of reporting it as though it were costs marks in every exam. Add 105 and every remainder comes back unchanged, because 105 is divisible by 3, by 5 and by 7. So 128 works. So does 233, 338, 443, and — going the other way — −82. What the theorem delivers is a residue class, written x ≡ 23 (mod 105), not a single integer. If the leftovers themselves are what you need to compute, the modulo calculator handles those directly.

A general method took considerably longer to appear than the answer did. Qin Jiushao published one in 1247 under the name “the great extension”, and Gauss gave the version every textbook now prints in his 1801 Disquisitiones Arithmeticae. Between those dates the problem was mostly a party trick. The modern statement covers arbitrary systems, not just three congruences with small moduli.

Why the Answer Repeats Every 105

Here is the counting argument, and it’s worth following because it explains the coprimality condition instead of just stating it. A number modulo 3 has 3 possible remainders, modulo 5 it has 5, modulo 7 it has 7. That gives 3 × 5 × 7 = 105 possible remainder triples. There are also exactly 105 residue classes modulo 105. Now map each number in 0…104 to its triple. No two of them can produce the same triple — if they did, their difference would be divisible by 3, by 5 and by 7 at once, hence by 105, which is impossible for two distinct numbers under 105. So 105 inputs map to 105 outputs with no collisions, which forces the map to hit every triple exactly once.

Every possible combination of remainders is therefore achievable, and achievable in one way. That is a stronger claim than “a solution exists”, and it depends entirely on the moduli being pairwise coprime. Once they share factors the neat count collapses:

ModuliRemainder patternsPatterns with a solutionRepeat period
3, 5, 7105105 (100%)105
4, 62412 (50%)12
6, 159030 (33%)30
2, 3, 4, 5, 6, 75,040420 (8%)420

Read the last two columns together: the number of solvable patterns always equals the repeat period, which is the least common multiple of the moduli. For coprime moduli the lcm equals the product and nothing is lost. For 2 through 7 the product is 5,040 but the lcm is only 420, so eleven out of every twelve remainder patterns you could write down are self-contradictory and have no solution at all. The classic basket-of-eggs puzzle — one egg left over in groups of 2, 3, 4, 5 and 6, none left over in groups of 7 — is one of the lucky patterns, and its answer is 301. Load that preset and the combined modulus tile reports 420 rather than 5,040.

Gauss’s Table: Three Numbers That Do the Whole Job

The classical construction builds the answer out of pieces that are deliberately blind to each other. Multiply all the moduli into M = 105. For each congruence take Mᵢ = M / nᵢ, which is divisible by every modulus except its own, then scale it by yᵢ = Mᵢ⁻¹ mod nᵢ so it lands on exactly 1 in its own congruence. Multiply by the remainder aᵢ and add everything up:

aᵢ (mod nᵢ)Mᵢ = 105 / nᵢMᵢ mod nᵢyᵢaᵢ · Mᵢ · yᵢ
2 (mod 3)3522140
3 (mod 5)211163
2 (mod 7)151130
Total, then reduced mod 105233 → 23

Check the first row against the second congruence to see the design: 140 = 28 × 5, so modulo 5 it contributes nothing, and the same holds modulo 7. Only the middle term speaks modulo 5. Each term is loud in one congruence and silent in the others, which is why the sum satisfies all three simultaneously.

The one genuinely fiddly column is yᵢ, and it is where hand-worked attempts go wrong. It is a modular inverse, not a fraction — you want the number that multiplies Mᵢ up to 1 modulo nᵢ. In row one that means solving 35y ≡ 1 (mod 3). Reduce 35 to 2 first, then 2 × 2 = 4 ≡ 1, so y = 2. Reducing before inverting saves real effort here, and for a modulus like 5 it turns a two-digit inversion into a single-digit one. This construction is also the reason the theorem demands coprime moduli: if Mᵢ and nᵢ shared a factor, that inverse would not exist and the table would stall on the fourth column.

What a Chinese Remainder Theorem Calculator Does Instead: Two Merges

Software rarely builds Gauss’s table. It folds the congruences together in pairs instead: solve the first two, replace them with the single congruence that captures both, then fold in the third. Sun Tzu’s system takes two merges.

Start with x ≡ 2 (mod 3). Bring in x ≡ 3 (mod 5). Anything satisfying the first has the form 2 + 3k, so 2 + 3k ≡ 3 (mod 5), which gives 3k ≡ 1 (mod 5) and k ≡ 2 (mod 5). Take k = 2 and you get x = 8, so the pair collapses to x ≡ 8 (mod 15). Now bring in x ≡ 2 (mod 7): numbers of the form 8 + 15k need 15k ≡ −6 (mod 7), which reduces to k ≡ 1 (mod 7), so k = 1 and x = 23. Two merges, no table, and the running modulus grows 3 → 15 → 105 as the information accumulates.

Both routes are traced in the calculator above — switch Method to trace to compare them on your own system. The merge route wins on scale, because each step is one gcd and one multiplication regardless of how many congruences remain, and it never needs the product of all moduli in one piece.

For a three-congruence homework problem, though, neither method beats sieving by hand — and the trick is to start from the largest modulus. List the numbers satisfying x ≡ 2 (mod 7): 2, 9, 16, 23. Check each against mod 5 = 3 and mod 3 = 2. You hit 23 on the fourth try. Starting from the smallest modulus instead means walking 2, 5, 8, 11, 14, 17, 20, 23 — twice the work, and the gap widens fast as the moduli grow, since the number of trials is capped by the combined modulus divided by the one you sieve on.

Moduli That Share a Factor Still Work — Sometimes

Most CRT calculators refuse any system whose moduli aren’t coprime. That refusal is overcautious: such systems often have solutions, they just need one extra test. Merging x ≡ r (mod M) with x ≡ a (mod n) succeeds exactly when gcd(M, n) divides a − r.

Take x ≡ 2 (mod 6) and x ≡ 8 (mod 15). The moduli share a factor of 3, so the classical construction is unavailable. The test: gcd(6, 15) = 3, and the gap 8 − 2 = 6 is divisible by 3. Solvable. The answer is x ≡ 8 (mod 30), where 30 is the lcm rather than the product 90. Both congruences secretly agree that x ≡ 2 (mod 3); they merely say it twice, and the overlap is exactly the factor 3 that the product would have double-counted.

Now change one digit: x ≡ 1 (mod 4) and x ≡ 2 (mod 6). The first forces x odd, the second forces it even. gcd(4, 6) = 2 does not divide 2 − 1 = 1, so the test fails, and no integer anywhere satisfies both — not a limitation of the method, a property of the system. Both cases are loaded as presets above so you can watch the merge table succeed and fail. This is also the situation where an answer that “looks fine” is most dangerous: a solver that skips the gcd test can return a number that satisfies the last congruence it processed and quietly violates an earlier one, which is precisely why the verification table checks the result against every row rather than the final one.

The Four-Fold Speedup Inside Every RSA Signature

RSA decryption computes m = cd mod n, where n = pq is 2048 bits and d is roughly the same size. Done directly, that is a modular exponentiation with 2048-bit operands: about 2048 squarings on numbers whose multiplication cost grows with the square of their length.

The private key holder knows p and q, so they can work in the two halves separately. Compute m₁ = cd mod (p−1) mod p and m₂ = cd mod (q−1) mod q, each with a 1024-bit modulus and a 1024-bit exponent, then recombine with CRT. Halving the operand length cuts each multiplication to a quarter of the cost and halves the number of squarings, so each half costs about an eighth of the full job. Two halves, and you land near a quarter of the original — the familiar figure of roughly 4× faster, and the reason RSA private keys are stored as five numbers (p, q, dP, dQ, qInv) rather than just d and n.

The recombination is a two-congruence CRT with the inverse precomputed: h = qInv · (m₁ − m₂) mod p, then m = m₂ + h·q. Load the RSA-CRT preset above and the merge table shows this exact shape on the toy key p = 61, q = 53 — two pieces mod 61 and mod 53 folding into one answer mod 3,233.

One warning, because it is a genuine and famous failure mode. If a hardware glitch corrupts just one of the two halves, the resulting signature is correct modulo one prime and wrong modulo the other — and gcd(m − m’, n) then hands an attacker the factorisation of n from a single faulty output. That is the Bellcore fault attack, published in 1997, and it is why production RSA implementations verify a CRT signature before releasing it. The same structural insight that makes CRT fast also makes a corrupted half catastrophic, which is a fair summary of why this seventeen-hundred-year-old counting trick still gets attention. If you would rather see the structure than the cryptography, the Euler phi calculator shows the same split-into-coprime-parts idea applied to counting instead of computing.

Frequently Asked Questions

Still Have Questions?

The detailed content on this page provides comprehensive explanations and examples to help you understand better.