Chinese Remainder Theorem šŸŽÆ

beginner
22 min

Chinese Remainder Theorem šŸŽÆ

Welcome to a fascinating journey into the heart of number theory! Today, we'll be exploring the Chinese Remainder Theorem – a powerful tool that solves simultaneous congruences, making it an essential skill for every programmer.

Understanding the Chinese Remainder Theorem šŸ“

The Chinese Remainder Theorem (CRT) is a method used to solve a system of linear congruences, connecting the solutions of individual congruences.

Here's a simple way to understand it:

If you have multiple numbers (moduli) that are pairwise coprime, you can find a single solution that works for all of them simultaneously. Let's dive deeper into the world of congruences and coprime numbers.

Congruences šŸ’”

In mathematics, a congruence is an equation of the form a ≔ b (mod m), where a and b are integers, m is a positive integer, and ≔ denotes congruence.

This means that a and b leave the same remainder when divided by m. For example:

  • 7 ≔ 2 (mod 5) because when you divide 7 by 5, the remainder is 2.
  • 9 ≢ 2 (mod 5) because when you divide 9 by 5, the remainder is not 2.

Coprime Numbers šŸ’”

Two numbers are called coprime or relatively prime if their greatest common divisor (GCD) is 1. In other words, they share no common factors other than 1.

For example:

  • 3 and 4 are coprime because their GCD is 1.
  • 6 and 9 are not coprime because their GCD is 3.

Why is the Chinese Remainder Theorem Important? šŸ’”

The CRT is essential in solving complex problems, such as scheduling jobs that require different amounts of time on multiple machines, or encoding messages in a way that can be decoded using multiple keys.

Now that we've covered the basics, let's move on to solving a system of linear congruences using the Chinese Remainder Theorem!

Solving a System of Linear Congruences šŸ’”

To solve a system of linear congruences using CRT, follow these steps:

  1. List the congruences and their moduli (m_i).
  2. Find the multiplicative inverse (modulo each m_i).
  3. Rearrange the congruences and their multiplicative inverses.
  4. Combine the congruences using the CRT formula.
  5. Solve the resulting congruence for a unique solution (x).

Example šŸ“

Let's solve the following system of linear congruences:

  • x ≔ 2 (mod 3)
  • x ≔ 3 (mod 4)

Step 1: List the congruences and their moduli šŸ“

  • x ≔ 2 (mod 3)
  • x ≔ 3 (mod 4)

Step 2: Find the multiplicative inverse (modulo each m_i) šŸ’”

  • For m_1 = 3, the multiplicative inverse of 3 is 2 (because 3 * 2 ≔ 1 (mod 3)).
  • For m_2 = 4, the multiplicative inverse of 3 is 3 (because 3 * 3 ≔ 1 (mod 4)).

Step 3: Rearrange the congruences and their multiplicative inverses šŸ’”

  • 2 * x ≔ 1 (mod 3)
  • 3 * x ≔ 1 (mod 4)

Step 4: Combine the congruences using the CRT formula šŸ’”

The CRT formula is:

x ≔ Ī£ (m_i * (s_i * t_i)) (mod Ī  (m_i))
  • Ī£ denotes summation.
  • m_i are the moduli (3 and 4 in this case).
  • s_i are the solutions to the congruences with the multiplicative inverses (2 and 3 in this case).
  • t_i are the time variables (1 for each congruence in this case).
  • Ī  denotes the product of the moduli.

Substituting our values:

x ≔ (3 * 2 * 1) + (4 * 3 * 1) (mod 3 * 4)

Step 5: Solve the resulting congruence for a unique solution (x) šŸ’”

x ≔ 6 + 12 (mod 12) x ≔ 18 (mod 12)

Now we have a solution for the system of linear congruences:

  • x ≔ 18 (mod 12)

Implementing the Chinese Remainder Theorem in Python šŸ“

Here's a Python implementation of the Chinese Remainder Theorem to solve more complex problems:

python
def mod_inverse(a, m): m0 = m new_a = a % m m2 = m0 while new_a != 1: q = m0 // new_a m1 = m0 % new_a m0, new_a = new_a, m1 m1, m2 = m2, m0 return m2 if m2 < m0 else m2 - m0 def chinese_remainder(n, rem): sum = 0 prod = reduce(lambda a, b: a * b, n) for n_i, r_i in zip(n, rem): p = prod // n_i sum += p * (r_i + mod_inverse(p, n_i) * (prod // n_i)) return sum % prod # Example usage moduli = [3, 4] remainders = [2, 3] print(chinese_remainder(moduli, remainders)) # Output: 18

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What does the Chinese Remainder Theorem do?

Quick Quiz
Question 1 of 1

What is a multiplicative inverse (modulo `m`)?