← All Tools

πŸ”— Chinese Remainder Theorem

Given a system of simultaneous congruences x ≑ a₁ (mod m₁), x ≑ aβ‚‚ (mod mβ‚‚), …, find the unique solution x modulo lcm(m₁, mβ‚‚, …) if one exists. Handles both the classic coprime case and the generalized non-coprime case (Gauss's extension), and uses BigInt so it survives RSA-sized numbers.

Solution x (smallest non-negative)
β€”
Modulus M = lcm(mα΅’)
β€”

The full solution set is x, x + M, x + 2M, … β€” every integer congruent to the shown value mod M.

Step-by-Step Derivation

Solving…

Why the theorem matters

First stated in the 3rd-century Sunzi Suanjing ("There are certain things whose number is unknown. If we count them by threes, we have two left over…"), the Chinese Remainder Theorem tells you that if the moduli are pairwise coprime, exactly one residue class modulo their product satisfies all the congruences. Modern uses: RSA-CRT decryption is up to 4Γ— faster than plain modular exponentiation by working modulo the two primes separately; error-correcting codes use CRT to reconstruct residues from redundant checks; interpolation algorithms (Karatsuba, FFT-based multiplication) piggyback on the same reconstruction trick.