Chinese Remainder Theorem Calculator

Enter your congruences in the form x ≡ a (mod n). The calculator will find the smallest non-negative integer x that satisfies all congruences, provided that the moduli n are pairwise coprime.

Introduction to the Chinese Remainder Theorem

The Chinese Remainder Theorem (CRT) is a powerful and elegant result from number theory. It provides a unique solution to a system of linear congruences, given that certain conditions are met. Imagine you have a number, and you know its remainder when divided by several different numbers. The CRT helps you find that original number.

Historically, the earliest known formulation of the theorem appeared in the 3rd-century AD mathematical treatise "Sunzi Suanjing" (The Mathematical Classic of Sun Zi). Sun Zi posed a problem: "There are certain things whose number is unknown. If we count them by threes, there are 2 left over; by fives, there are 3 left over; and by sevens, there are 2 left over. How many things are there?" This classic puzzle is a perfect illustration of the CRT in action.

Understanding Congruences

Before diving into the theorem, let's briefly review what a congruence means. A statement like x ≡ a (mod n) means that x and a have the same remainder when divided by n. In other words, x - a is a multiple of n. For example:

  • x ≡ 2 (mod 3) means x could be 2, 5, 8, 11, ... (numbers that leave a remainder of 2 when divided by 3).
  • x ≡ 3 (mod 5) means x could be 3, 8, 13, 18, ... (numbers that leave a remainder of 3 when divided by 5).

The Chinese Remainder Theorem deals with finding a single number x that satisfies *multiple* such conditions simultaneously.

The Theorem Explained

The Chinese Remainder Theorem states that if you have a system of congruences:

  • x ≡ a₁ (mod n₁)
  • x ≡ a₂ (mod n₂)
  • ...
  • x ≡ aₖ (mod nₖ)

and if the moduli n₁, n₂, ..., nₖ are pairwise coprime (meaning that the greatest common divisor of any two different moduli is 1), then there exists a unique solution for x modulo N = n₁ * n₂ * ... * nₖ. The smallest non-negative solution is often the one sought.

Steps to Solve using CRT:

  1. Calculate N: Multiply all the moduli together: N = n₁ * n₂ * ... * nₖ.
  2. Calculate Nᵢ for each congruence: For each i, calculate Nᵢ = N / nᵢ.
  3. Find Modular Inverses (xᵢ): For each i, find an integer xᵢ such that Nᵢ * xᵢ ≡ 1 (mod nᵢ). This xᵢ is the modular multiplicative inverse of Nᵢ modulo nᵢ. It can be found using the Extended Euclidean Algorithm.
  4. Calculate the Solution: The solution x is given by the sum: x = (a₁N₁x₁ + a₂N₂x₂ + ... + aₖNₖxₖ) (mod N).
  5. Find Smallest Non-Negative Solution: If the calculated x is negative, add N to it until it's non-negative. If it's greater than or equal to N, take it modulo N.

Worked Example (Sun Zi's Problem):

x ≡ 2 (mod 3)

x ≡ 3 (mod 5)

x ≡ 2 (mod 7)

  1. N = 3 * 5 * 7 = 105
    • N₁ = 105 / 3 = 35
    • N₂ = 105 / 5 = 21
    • N₃ = 105 / 7 = 15
    • For n₁=3: Find x₁ such that 35x₁ ≡ 1 (mod 3). Since 35 ≡ 2 (mod 3), we need 2x₁ ≡ 1 (mod 3). x₁ = 2 (because 2*2 = 4 ≡ 1 (mod 3)).
    • For n₂=5: Find x₂ such that 21x₂ ≡ 1 (mod 5). Since 21 ≡ 1 (mod 5), we need 1x₂ ≡ 1 (mod 5). x₂ = 1.
    • For n₃=7: Find x₃ such that 15x₃ ≡ 1 (mod 7). Since 15 ≡ 1 (mod 7), we need 1x₃ ≡ 1 (mod 7). x₃ = 1.
  2. x = (a₁N₁x₁ + a₂N₂x₂ + a₃N₃x₃) (mod N)

    x = (2 * 35 * 2 + 3 * 21 * 1 + 2 * 15 * 1) (mod 105)

    x = (140 + 63 + 30) (mod 105)

    x = (233) (mod 105)

  3. 233 = 2 * 105 + 23

    So, x ≡ 23 (mod 105). The smallest non-negative solution is 23.

Applications of the Chinese Remainder Theorem

The CRT is not just a mathematical curiosity; it has practical applications in various fields:

  • Cryptography: It's fundamental to the RSA public-key cryptosystem, speeding up computations for decryption and signature generation.
  • Error Detection and Correction: Used in coding theory to construct error-correcting codes.
  • Computer Science: Facilitates arbitrary-precision arithmetic for very large integers by breaking down large number operations into smaller, more manageable ones. It's also used in hashing and secret sharing schemes.
  • Astronomy and Calendrical Calculations: Historically, it was used to determine cycles of astronomical events and in calendar constructions.
  • Scheduling: Can be applied to problems involving periodic events that need to synchronize.

How to Use This Calculator

Our Chinese Remainder Theorem Calculator simplifies the process of finding solutions to systems of congruences. Here's how to use it:

  1. Input Remainders (a) and Moduli (n): For each congruence x ≡ a (mod n), enter the remainder a in the first input box and the modulus n in the second input box.
  2. Add/Remove Congruences:
    • Click "Add Congruence" to add another pair of input fields for a new congruence.
    • Click the "Remove" button next to a congruence to delete it.
  3. Calculate: Once all your congruences are entered, click the "Calculate" button.
  4. View Results: The calculator will display the smallest non-negative integer solution x and the overall modulus N (the product of all individual moduli). If there's an issue, such as non-coprime moduli, an error message will appear.

Limitations and Advanced Cases

This calculator, like the standard CRT, primarily handles systems where the moduli n₁, n₂, ..., nₖ are pairwise coprime. If the moduli are not pairwise coprime, a solution might still exist, but the standard CRT algorithm needs modification or a generalized version (like the General Chinese Remainder Theorem) is required. In such cases, a solution exists if and only if aᵢ ≡ aⱼ (mod gcd(nᵢ, nⱼ)) for all pairs i, j. If this condition holds, the solution is unique modulo lcm(n₁, n₂, ..., nₖ).

For simplicity, this calculator will indicate an error if non-coprime moduli are detected, prompting you to ensure your inputs meet the standard CRT requirements.

Further Reading

To deepen your understanding of number theory and its applications, consider exploring:

  • Extended Euclidean Algorithm
  • Modular Arithmetic
  • Group Theory in Cryptography
  • Generalized Chinese Remainder Theorem