From agent-almanac
Solves modular arithmetic problems: linear congruences, Chinese Remainder Theorem systems, modular inverses, large exponentiations via Euler's theorem. For number theory computations.
npx claudepluginhub pjt222/agent-almanacThis skill uses the workspace's default tool permissions.
---
Solves Diophantine equations for integer solutions to linear, quadratic, Pell equations, and Pythagorean triples. Finds particular/general solutions or proves non-existence via modular constraints.
Provides patterns for math/combinatorics problems: modular arithmetic, prime sieves, GCD/LCM, binomial coefficients, fast exponentiation, counting techniques, inclusion-exclusion, game theory. For number theory/combinatorics.
Performs symbolic math in Python using SymPy: solves equations algebraically, computes derivatives/integrals/limits, simplifies expressions, handles matrices, physics, and generates code.
Share bugs, ideas, or general feedback.
Solve modular arithmetic problems by parsing congruence systems, applying the extended Euclidean algorithm for inverses, using the Chinese Remainder Theorem for simultaneous congruences, and leveraging Euler's theorem for modular exponentiation. Verify every solution by substitution.
Extract the mathematical structure from the problem statement.
Identify the type:
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.
Expected: A clearly parsed and normalized modular problem with all values reduced.
On failure: If the notation is ambiguous (e.g., "solve 3x + 5 = 2 mod 7" could mean 3x + 5 = 2 (mod 7) or 3x + (5 = 2 mod 7)), clarify with the user. Default to interpreting mod as applying to the entire equation.
Solve ax = b (mod m) using the extended Euclidean algorithm.
Compute g = gcd(a, m) using the Euclidean algorithm:
Check solvability: ax = b (mod m) has a solution if and only if g | b.
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:
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).
Expected: The complete solution set for the congruence, or a proof that no solution exists.
On failure: If the extended Euclidean back-substitution produces the wrong result, verify each division step. The most common error is a sign mistake during back-substitution. Check: a * inverse mod m should equal 1.
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.
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 |
Expected: A unique solution modulo M, or a proof of incompatibility.
On failure: If the CRT computation yields a result that fails verification, check the modular inverse computations in step 4. A common mistake is computing Mi^{-1} mod M instead of Mi^{-1} mod mi. Each inverse is computed modulo the individual modulus, not the product.
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).
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 a^r (mod m) using repeated squaring (binary exponentiation):
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.
Expected: The value of a^k (mod m), computed via exponent reduction and repeated squaring.
On failure: If gcd(a, m) > 1 and the result seems wrong, do not apply Euler's theorem. Instead, compute directly or factor m into coprime parts where at least some parts are coprime to a, solve modulo each part, and recombine with CRT.
Check every solution by plugging it back into the 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.
Expected: All original equations verified with explicit computation shown.
On failure: If verification fails, trace back through the procedure to find the computational error. Common sources: arithmetic mistakes in the extended Euclidean algorithm, wrong sign in back-substitution, or forgetting to reduce modulo M in the final CRT step.
Applying CRT without coprimality check: The standard CRT formula requires pairwise coprime moduli. Applying it to non-coprime moduli gives a wrong answer, not an error. Always check gcd(mi, mj) = 1 first.
Computing the wrong inverse: Mi^{-1} must be computed modulo mi (the individual modulus), not modulo M (the product). This is the single most common CRT implementation error.
Applying Euler's theorem when gcd(a, m) > 1: a^{phi(m)} = 1 (mod m) requires gcd(a, m) = 1. If this fails, the theorem does not apply and the result is wrong.
Sign errors in extended Euclidean back-substitution: Keep careful track of signs at each step. The final inverse may be negative; always reduce modulo m to get a positive representative.
Overflow in modular exponentiation: Even with repeated squaring, intermediate products can overflow. Always reduce modulo m after every multiplication, not just at the end.
Forgetting 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.
analyze-prime-numbers -- Prime factorization is needed to compute phi(m) and to verify coprimalityexplore-diophantine-equations -- Linear Diophantine equations ax + by = c are 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)