Chinese Remainder Theorem Calculator

Solve x ≡ a₁ (mod m₁), x ≡ a₂ (mod m₂), … with steps. Works when the moduli share factors too, and says when the congruences contradict.

Whole numbers separated by commas or spaces, up to 8.
One modulus (1 or more) for each remainder, in the same order. Up to 30 digits each. They do not need to be coprime.

Results

Solution
x ≡ 23 (mod 30)
Smallest non-negative solution
23
Every solution
x = 23 + 30k for any whole number k: 23, 53, 83, 113, 143, …
Combined modulus
30: the moduli are pairwise coprime, so this is their product.
Steps (combining one congruence at a time)
Start with x ≡ 1 (mod 2), so x = 1 + 2k. Add x ≡ 2 (mod 3): 1 + 2k ≡ 2, so 2k ≡ 1 (mod 3). gcd(2, 3) = 1, so multiply by 2⁻¹ ≡ 2 (mod 3): k ≡ 2 (mod 3). x = 1 + 2 × 2 = 5, so x ≡ 5 (mod 6). Add x ≡ 3 (mod 5): 5 + 6k ≡ 3, so 6k ≡ −2 (mod 5). gcd(6, 5) = 1, so multiply by 6⁻¹ ≡ 1 (mod 5): k ≡ 3 (mod 5). x = 5 + 6 × 3 = 23, so x ≡ 23 (mod 30).
System solved
x ≡ 1 (mod 2), x ≡ 2 (mod 3), x ≡ 3 (mod 5)

How it’s calculated

The Chinese remainder theorem says that if the moduli m₁, m₂, … are pairwise coprime (no two share a factor), the system x ≡ a₁ (mod m₁), x ≡ a₂ (mod m₂), … always has exactly one solution modulo N = m₁ × m₂ × …. The textbook formula is

x ≡ a₁N₁y₁ + a₂N₂y₂ + … (mod N), where Nᵢ = N ÷ mᵢ and yᵢ ≡ Nᵢ⁻¹ (mod mᵢ)

This calculator combines the congruences one at a time instead, which also works when the moduli share factors. From x = a + Mk, the next congruence x ≡ b (mod n) becomes Mk ≡ b − a (mod n). With g = gcd(M, n):

Example: x ≡ 1 (mod 2), x ≡ 2 (mod 3), x ≡ 3 (mod 5). N = 30, N₁ = 15, N₂ = 10, N₃ = 6, and each yᵢ is 1, so x ≡ 1×15 + 2×10 + 3×6 = 53 ≡ 23 (mod 30). Check: 23 is odd, 23 = 7×3 + 2 and 23 = 4×5 + 3. With shared factors, x ≡ 4 (mod 6) and x ≡ 2 (mod 8) gives 6k ≡ −2 (mod 8), so 3k ≡ −1 (mod 4), k ≡ 1, and x ≡ 10 (mod 24).

Frequently asked questions

What if the moduli are not coprime?

There is a solution exactly when every pair of remainders agrees modulo the GCD of their moduli, and it is unique modulo the LCM of the moduli. For example x ≡ 1 (mod 15) and x ≡ 2 (mod 35) has no solution, because 1 and 2 differ modulo gcd(15, 35) = 5.

Why is the answer a whole family of numbers?

If x works, so does x plus any multiple of the combined modulus, because adding it does not change any of the remainders. The calculator shows the smallest non-negative one and the pattern.

Can a remainder be bigger than its modulus or negative?

Yes. x ≡ 17 (mod 5) means the same as x ≡ 2 (mod 5), and x ≡ −1 (mod 5) means x ≡ 4 (mod 5). The calculator reduces them first.

What is a classic CRT puzzle?

Find a number that leaves remainder 2 when divided by 3, 3 when divided by 5 and 2 when divided by 7. Enter remainders 2, 3, 2 and moduli 3, 5, 7: the answer is 23 (mod 105).

Embed this calculator

Add this free calculator to your own website. Copy the code below into your page’s HTML:

Need this calculator branded, changed or built around your own prices?See custom business solutions →

Sources

Formulas are taken from the free public references above. Results are provided “as is” for informational and educational purposes only. See our disclaimer.

Added · Last updated · How we check our calculators

Spotted a mistake or missing option? Report a problem· Suggest a calculator