Solve Modular Arithmetic
Solve modular arithmetic problems. Parse congruence systems. Apply extended Euclidean algorithm for inverses. Use Chinese Remainder Theorem for simultaneous congruences. Leverage Euler's theorem for modular exponentiation. Verify every solution by substitution.
When Use
- Solve single linear congruence ax = b (mod m)
- Solve system of simultaneous congruences (Chinese Remainder Theorem)
- Compute modular inverse a^{-1} (mod m)
- Evaluate large modular exponentiations a^k (mod m)
- Determine order of element in Z/mZ
- Work with cyclic groups, primitive roots, or discrete logarithm contexts
Inputs
- Required: Congruence(s) or modular equation to solve
- Optional: Whether to show extended Euclidean algorithm steps explicit
- Optional: Whether Euler's theorem or Fermat's little theorem should be applied
- Optional: Whether to find primitive roots or element orders
- Optional: Output format (step-by-step, compact, or proof-style)
Steps
Step 1: Parse Congruence System or Modular Equation
Extract mathematical structure from problem statement.
Identify the type:
- Single linear congruence: ax = b (mod m)
- System of congruences: x = a1 (mod m1), x = a2 (mod m2), ...
- Modular exponentiation: a^k (mod m)
- Modular inverse: find a^{-1} (mod m)
Normalize: Reduce all coefficients modulo their respective moduli. Ensure a, b, m are non-negative integers with m > 0.
Record the parsed problem in standard notation.
Got: Clear parsed and normalized modular problem with all values reduced.
If fail: Notation ambiguous (e.g., "solve 3x + 5 = 2 mod 7" could mean 3x + 5 = 2 (mod 7) or 3x + (5 = 2 mod 7))? Clarify with user. Default to interpreting mod as applying to entire equation.
Step 2: Solve Single Congruence (if applicable)
Solve ax = b (mod m) using extended Euclidean algorithm.
Compute g = gcd(a, m) using the Euclidean algorithm:
- Apply repeated division: m = q1a + r1, a = q2r1 + r2, ... until remainder = 0.
- The last non-zero remainder is gcd(a, m).
Check solvability: ax = b (mod m) has a solution if and only if g | b.
- If g does not divide b, the congruence has no solution. Stop.
Reduce: Divide through by g to get (a/g)x = (b/g) (mod m/g). Now gcd(a/g, m/g) = 1.
Find the modular inverse of a/g modulo m/g using the extended Euclidean algorithm:
- Back-substitute through the Euclidean algorithm steps to express gcd as a linear combination: 1 = (a/g)*s + (m/g)*t.
- The coefficient s (reduced mod m/g) is the inverse.
Compute the particular solution: x0 = s * (b/g) mod (m/g).
Write the general solution: x = x0 + (m/g)*k for k = 0, 1, ..., g - 1 gives all g incongruent solutions modulo m.
Extended Euclidean algorithm example (finding 17^{-1} mod 43):
43 = 2*17 + 9
17 = 1*9 + 8
9 = 1*8 + 1
8 = 8*1 + 0
Back-substitute:
1 = 9 - 1*8
= 9 - 1*(17 - 1*9) = 2*9 - 17
= 2*(43 - 2*17) - 17 = 2*43 - 5*17
So 17*(-5) = 1 (mod 43), i.e., 17^{-1} = -5 = 38 (mod 43).
Got: Complete solution set for congruence, or proof no solution exists.
If fail: Extended Euclidean back-substitution produces wrong result? Verify each division step. Most common error: sign mistake during back-substitution. Check: a * inverse mod m should equal 1.
Step 3: Solve System via Chinese Remainder Theorem (if applicable)
Solve x = a1 (mod m1), x = a2 (mod m2), ..., x = ak (mod mk).
Check pairwise coprimality: For every pair (mi, mj), verify gcd(mi, mj) = 1.
- If all pairs are coprime, CRT applies directly.
- If some pairs are not coprime, check compatibility: for each non-coprime pair, verify ai = aj (mod gcd(mi, mj)). If compatible, reduce using lcm. If incompatible, no solution exists.
Compute M = m1 * m2 * ... * mk (the product of all moduli).
For each i, compute Mi = M / mi (the product of all moduli except mi).
For each i, find yi = Mi^{-1} (mod mi) using the extended Euclidean algorithm from Step 2.
Compute the solution: x = sum(ai * Mi * yi for i = 1..k) mod M.
State the result: x = [value] (mod M). This is the unique solution modulo M.
Common totients reference:
| n |
phi(n) |
n |
phi(n) |
n |
phi(n) |
| 2 |
1 |
10 |
4 |
20 |
8 |
| 3 |
2 |
11 |
10 |
24 |
8 |
| 4 |
2 |
12 |
4 |
25 |
20 |
| 5 |
4 |
13 |
12 |
30 |
8 |
| 6 |
2 |
14 |
6 |
36 |
12 |
| 7 |
6 |
15 |
8 |
48 |
16 |
| 8 |
4 |
16 |
8 |
60 |
16 |
| 9 |
6 |
18 |
6 |
100 |
40 |
Got: Unique solution modulo M, or proof of incompatibility.
If fail: CRT computation yields result that fails verification? Check modular inverse computations in step 4. Common mistake: computing Mi^{-1} mod M instead of Mi^{-1} mod mi. Each inverse is computed modulo individual modulus, not the product.
Step 4: Apply Euler's Theorem or Fermat's Little Theorem (if applicable)
Evaluate modular exponentiations or simplify expressions using Euler's theorem.
Euler's theorem: If gcd(a, m) = 1, then a^{phi(m)} = 1 (mod m).
- Compute phi(m) using the totient formula: if m = p1^e1 * p2^e2 * ... * pk^ek, then phi(m) = m * product((1 - 1/pi) for each prime pi dividing m).
Fermat's little theorem (special case): If p is prime and gcd(a, p) = 1, then a^{p-1} = 1 (mod p).
Reduce the exponent: To compute a^k (mod m):
- Compute r = k mod phi(m).
- Then a^k = a^r (mod m).
Compute a^r (mod m) using repeated squaring (binary exponentiation):
- Write r in binary: r = b_n * 2^n + ... + b_1 * 2 + b_0.
- Start with result = 1.
- For each bit from most significant to least: result = result^2 mod m; if bit is 1, result = result * a mod m.
Handle the case gcd(a, m) > 1: Euler's theorem does not apply directly. Factor m and use CRT to combine results from prime power moduli, using lifting the exponent or direct computation.
Got: Value of a^k (mod m), computed via exponent reduction and repeated squaring.
If fail: gcd(a, m) > 1 and result seems wrong? Never apply Euler's theorem. Instead, compute direct or factor m into coprime parts where at least some parts are coprime to a, solve modulo each part, recombine with CRT.
Step 5: Verify Solution by Substitution
Check every solution by plugging back into original equations.
For single congruences: Compute a * x mod m and verify it equals b.
For CRT systems: For each congruence x = ai (mod mi), verify x mod mi = ai.
For modular exponentiations: If possible, verify with a second computational method (e.g., direct computation for small values, or independent repeated squaring implementation).
Document the verification explicitly:
Solution: x = 23
Check 1: 23 mod 3 = 2 = a1. Correct.
Check 2: 23 mod 5 = 3 = a2. Correct.
Check 3: 23 mod 7 = 2 = a3. Correct.
All congruences satisfied.
Got: All original equations verified with explicit computation shown.
If fail: Verification fails? Trace back through procedure to find computational error. Common sources: arithmetic mistakes in extended Euclidean algorithm, wrong sign in back-substitution, or forgetting reduce modulo M in final CRT step.
Checks
Pitfalls
Apply CRT without coprimality check: Standard CRT formula needs pairwise coprime moduli. Apply to non-coprime moduli? Wrong answer, not error. Always check gcd(mi, mj) = 1 first.
Compute wrong inverse: Mi^{-1} must be computed modulo mi (the individual modulus), not modulo M (the product). Single most common CRT implementation error.
Apply Euler's theorem when gcd(a, m) > 1: a^{phi(m)} = 1 (mod m) needs gcd(a, m) = 1. Fails? Theorem does not apply, result wrong.
Sign errors in extended Euclidean back-substitution: Keep careful track of signs at each step. Final inverse may be negative; always reduce modulo m to get positive representative.
Overflow in modular exponentiation: Even with repeated squaring, intermediate products can overflow. Always reduce modulo m after every multiplication, not just at end.
Forget multiple solutions: ax = b (mod m) with g = gcd(a, m) > 1 and g | b has exactly g incongruent solutions modulo m, not just one.
See Also
analyze-prime-numbers -- Prime factorization needed to compute phi(m) and to verify coprimality
explore-diophantine-equations -- Linear Diophantine equations ax + by = c equivalent to linear congruences ax = c (mod b)
prove-geometric-theorem -- Modular arithmetic appears in constructibility proofs (e.g., which regular n-gons are constructible)
1---2name: solve-modular-arithmetic-63description: Solve modular arithmetic problems including congruences, systems via the Chinese Remainder Theorem, modular inverses, and Euler's theorem applications. Covers both manual and computational approaches. Use when solving linear congruences, computing modular inverses, evaluating large modular exponentiations, working with simultaneous congruences (CRT), or operating in cyclic groups and discrete logarithm contexts.4license: MIT5---67# Solve Modular Arithmetic89Solve modular arithmetic problems. Parse congruence systems. Apply extended Euclidean algorithm for inverses. Use Chinese Remainder Theorem for simultaneous congruences. Leverage Euler's theorem for modular exponentiation. Verify every solution by substitution.1011## When Use1213- Solve single linear congruence ax = b (mod m)14- Solve system of simultaneous congruences (Chinese Remainder Theorem)15- Compute modular inverse a^{-1} (mod m)16- Evaluate large modular exponentiations a^k (mod m)17- Determine order of element in Z/mZ18- Work with cyclic groups, primitive roots, or discrete logarithm contexts1920## Inputs2122- **Required**: Congruence(s) or modular equation to solve23- **Optional**: Whether to show extended Euclidean algorithm steps explicit24- **Optional**: Whether Euler's theorem or Fermat's little theorem should be applied25- **Optional**: Whether to find primitive roots or element orders26- **Optional**: Output format (step-by-step, compact, or proof-style)2728## Steps2930### Step 1: Parse Congruence System or Modular Equation3132Extract mathematical structure from problem statement.33341. **Identify the type**:35 - Single linear congruence: ax = b (mod m)36 - System of congruences: x = a1 (mod m1), x = a2 (mod m2), ...37 - Modular exponentiation: a^k (mod m)38 - Modular inverse: find a^{-1} (mod m)39402. **Normalize**: Reduce all coefficients modulo their respective moduli. Ensure a, b, m are non-negative integers with m > 0.41423. **Record** the parsed problem in standard notation.4344**Got:** Clear parsed and normalized modular problem with all values reduced.4546**If fail:** Notation ambiguous (e.g., "solve 3x + 5 = 2 mod 7" could mean 3x + 5 = 2 (mod 7) or 3x + (5 = 2 mod 7))? Clarify with user. Default to interpreting mod as applying to entire equation.4748### Step 2: Solve Single Congruence (if applicable)4950Solve ax = b (mod m) using extended Euclidean algorithm.51521. **Compute g = gcd(a, m)** using the Euclidean algorithm:53 - Apply repeated division: m = q1*a + r1, a = q2*r1 + r2, ... until remainder = 0.54 - The last non-zero remainder is gcd(a, m).55562. **Check solvability**: ax = b (mod m) has a solution if and only if g | b.57 - If g does not divide b, the congruence has no solution. Stop.58593. **Reduce**: Divide through by g to get (a/g)x = (b/g) (mod m/g). Now gcd(a/g, m/g) = 1.60614. **Find the modular inverse** of a/g modulo m/g using the extended Euclidean algorithm:62 - Back-substitute through the Euclidean algorithm steps to express gcd as a linear combination: 1 = (a/g)*s + (m/g)*t.63 - The coefficient s (reduced mod m/g) is the inverse.64655. **Compute the particular solution**: x0 = s * (b/g) mod (m/g).66676. **Write the general solution**: x = x0 + (m/g)*k for k = 0, 1, ..., g - 1 gives all g incongruent solutions modulo m.6869**Extended Euclidean algorithm example (finding 17^{-1} mod 43):**70```text7143 = 2*17 + 97217 = 1*9 + 873 9 = 1*8 + 174 8 = 8*1 + 07576Back-substitute:771 = 9 - 1*878 = 9 - 1*(17 - 1*9) = 2*9 - 1779 = 2*(43 - 2*17) - 17 = 2*43 - 5*178081So 17*(-5) = 1 (mod 43), i.e., 17^{-1} = -5 = 38 (mod 43).82```8384**Got:** Complete solution set for congruence, or proof no solution exists.8586**If fail:** Extended Euclidean back-substitution produces wrong result? Verify each division step. Most common error: sign mistake during back-substitution. Check: a * inverse mod m should equal 1.8788### Step 3: Solve System via Chinese Remainder Theorem (if applicable)8990Solve x = a1 (mod m1), x = a2 (mod m2), ..., x = ak (mod mk).91921. **Check pairwise coprimality**: For every pair (mi, mj), verify gcd(mi, mj) = 1.93 - If all pairs are coprime, CRT applies directly.94 - If some pairs are not coprime, check compatibility: for each non-coprime pair, verify ai = aj (mod gcd(mi, mj)). If compatible, reduce using lcm. If incompatible, no solution exists.95962. **Compute M = m1 * m2 * ... * mk** (the product of all moduli).97983. **For each i, compute Mi = M / mi** (the product of all moduli except mi).991004. **For each i, find yi = Mi^{-1} (mod mi)** using the extended Euclidean algorithm from Step 2.1011025. **Compute the solution**: x = sum(ai * Mi * yi for i = 1..k) mod M.1031046. **State the result**: x = [value] (mod M). This is the unique solution modulo M.105106**Common totients reference:**107108| n | phi(n) | n | phi(n) | n | phi(n) |109|---|---|---|---|---|---|110| 2 | 1 | 10 | 4 | 20 | 8 |111| 3 | 2 | 11 | 10 | 24 | 8 |112| 4 | 2 | 12 | 4 | 25 | 20 |113| 5 | 4 | 13 | 12 | 30 | 8 |114| 6 | 2 | 14 | 6 | 36 | 12 |115| 7 | 6 | 15 | 8 | 48 | 16 |116| 8 | 4 | 16 | 8 | 60 | 16 |117| 9 | 6 | 18 | 6 | 100 | 40 |118119**Got:** Unique solution modulo M, or proof of incompatibility.120121**If fail:** CRT computation yields result that fails verification? Check modular inverse computations in step 4. Common mistake: computing Mi^{-1} mod M instead of Mi^{-1} mod mi. Each inverse is computed modulo *individual* modulus, not the product.122123### Step 4: Apply Euler's Theorem or Fermat's Little Theorem (if applicable)124125Evaluate modular exponentiations or simplify expressions using Euler's theorem.1261271. **Euler's theorem**: If gcd(a, m) = 1, then a^{phi(m)} = 1 (mod m).128 - Compute phi(m) using the totient formula: if m = p1^e1 * p2^e2 * ... * pk^ek, then phi(m) = m * product((1 - 1/pi) for each prime pi dividing m).1291302. **Fermat's little theorem** (special case): If p is prime and gcd(a, p) = 1, then a^{p-1} = 1 (mod p).1311323. **Reduce the exponent**: To compute a^k (mod m):133 - Compute r = k mod phi(m).134 - Then a^k = a^r (mod m).1351364. **Compute a^r (mod m)** using repeated squaring (binary exponentiation):137 - Write r in binary: r = b_n * 2^n + ... + b_1 * 2 + b_0.138 - Start with result = 1.139 - For each bit from most significant to least: result = result^2 mod m; if bit is 1, result = result * a mod m.1401415. **Handle the case gcd(a, m) > 1**: Euler's theorem does not apply directly. Factor m and use CRT to combine results from prime power moduli, using lifting the exponent or direct computation.142143**Got:** Value of a^k (mod m), computed via exponent reduction and repeated squaring.144145**If fail:** gcd(a, m) > 1 and result seems wrong? Never apply Euler's theorem. Instead, compute direct or factor m into coprime parts where at least some parts are coprime to a, solve modulo each part, recombine with CRT.146147### Step 5: Verify Solution by Substitution148149Check every solution by plugging back into original equations.1501511. **For single congruences**: Compute a * x mod m and verify it equals b.1521532. **For CRT systems**: For each congruence x = ai (mod mi), verify x mod mi = ai.1541553. **For modular exponentiations**: If possible, verify with a second computational method (e.g., direct computation for small values, or independent repeated squaring implementation).1561574. **Document the verification** explicitly:158```text159Solution: x = 23160Check 1: 23 mod 3 = 2 = a1. Correct.161Check 2: 23 mod 5 = 3 = a2. Correct.162Check 3: 23 mod 7 = 2 = a3. Correct.163All congruences satisfied.164```165166**Got:** All original equations verified with explicit computation shown.167168**If fail:** Verification fails? Trace back through procedure to find computational error. Common sources: arithmetic mistakes in extended Euclidean algorithm, wrong sign in back-substitution, or forgetting reduce modulo M in final CRT step.169170## Checks171172- [ ] Problem type correct identified (single congruence, system, exponentiation, inverse)173- [ ] All coefficients reduced modulo their respective moduli174- [ ] For ax = b (mod m): gcd(a, m) | b checked before solving175- [ ] Extended Euclidean algorithm back-substitution verified: a * inverse mod m = 1176- [ ] For CRT: pairwise coprimality verified before applying theorem177- [ ] For CRT with non-coprime moduli: compatibility checked178- [ ] Euler's theorem applied only when gcd(a, m) = 1179- [ ] Totient phi(m) computed from prime factorization, not guessed180- [ ] Repeated squaring uses modular reduction at every step (no overflow)181- [ ] Every solution verified by substitution into original equations182183## Pitfalls184185- **Apply CRT without coprimality check**: Standard CRT formula needs pairwise coprime moduli. Apply to non-coprime moduli? Wrong answer, not error. Always check gcd(mi, mj) = 1 first.186187- **Compute wrong inverse**: Mi^{-1} must be computed modulo mi (the *individual* modulus), not modulo M (the product). Single most common CRT implementation error.188189- **Apply Euler's theorem when gcd(a, m) > 1**: a^{phi(m)} = 1 (mod m) needs gcd(a, m) = 1. Fails? Theorem does not apply, result wrong.190191- **Sign errors in extended Euclidean back-substitution**: Keep careful track of signs at each step. Final inverse may be negative; always reduce modulo m to get positive representative.192193- **Overflow in modular exponentiation**: Even with repeated squaring, intermediate products can overflow. Always reduce modulo m after every multiplication, not just at end.194195- **Forget multiple solutions**: ax = b (mod m) with g = gcd(a, m) > 1 and g | b has exactly g incongruent solutions modulo m, not just one.196197## See Also198199- `analyze-prime-numbers` -- Prime factorization needed to compute phi(m) and to verify coprimality200- `explore-diophantine-equations` -- Linear Diophantine equations ax + by = c equivalent to linear congruences ax = c (mod b)201- `prove-geometric-theorem` -- Modular arithmetic appears in constructibility proofs (e.g., which regular n-gons are constructible)