How the Chinese Remainder Theorem works
The Chinese Remainder Theorem (CRT) says that if you know the remainders an unknown integer x leaves when divided by several pairwise coprime moduli, those remainders together pin down x uniquely, modulo the product of the moduli. This calculator solves a system of three congruences of the form x ≡ a (mod n) and returns the smallest non-negative x that satisfies all of them at once.
The formula
Given x ≡ a₁ (mod n₁), x ≡ a₂ (mod n₂), and x ≡ a₃ (mod n₃), where n₁, n₂, n₃ are pairwise coprime (gcd(ni, nj) = 1 for every pair), the classical construction builds the solution as follows:
- Combined modulus: N = n₁ × n₂ × n₃
- For each i, let Ni = N / ni and let Mi be the modular inverse of Ni mod ni (the value satisfying Ni × Mi ≡ 1 mod ni, found with the extended Euclidean algorithm)
- Solution: x = (a₁N₁M₁ + a₂N₂M₂ + a₃N₃M₃) mod N
The x this produces is the unique solution in the range 0 to N−1. Every other integer satisfying all three congruences differs from it by a multiple of N — that is, the full solution set is x, x + N, x + 2N, and so on (as well as x − N, x − 2N, ... in the negative direction).
Why the moduli must be coprime
The construction above only works when every pair of moduli shares no common factor. If two moduli share a factor greater than 1, a modular inverse may not exist and the "unique modulo N" guarantee breaks down — the system might have no solution at all, or the standard formula gives a wrong answer. This calculator checks gcd(ni, nj) for every pair before solving and flags the input if any pair is not coprime.
A worked example
The default values on this page — remainder 2 mod 3, remainder 3 mod 5, remainder 2 mod 7 — are the classic problem from the 3rd–5th century Chinese text Sunzi Suanjing. Here N = 3 × 5 × 7 = 105, and the unique solution is x = 23: check that 23 = 7×3 + 2, 23 = 4×5 + 3, and 23 = 3×7 + 2. Every number of the form 23 + 105k, such as 128 or −82, also satisfies all three congruences.
Applications
Beyond puzzle problems, the Chinese Remainder Theorem is a real computational tool. It speeds up modular exponentiation in RSA cryptography, where a private-key operation can be done separately modulo the two secret primes p and q, then recombined with CRT — several times faster than working modulo n = p×q directly. It also appears in scheduling problems with repeating cycles and in some checksum and error-correction schemes.