From agent-almanac
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.
npx claudepluginhub pjt222/agent-almanacThis skill uses the workspace's default tool permissions.
---
Solves modular arithmetic problems: linear congruences, Chinese Remainder Theorem systems, modular inverses, large exponentiations via Euler's theorem. For number theory computations.
Performs symbolic mathematics in Python using SymPy: algebraic equation solving, calculus (derivatives, integrals, limits), expression manipulation, matrices, physics, code generation. Use for exact symbolic results.
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 Diophantine equations -- polynomial equations where only integer solutions are sought. Classify the equation by type, test for solvability, find particular and general solutions, and generate solution families. Covers linear equations, Pell equations, Pythagorean triples, and general quadratic forms.
Determine the structure of the Diophantine equation to select the appropriate solving method.
Linear: ax + by = c where a, b, c are given integers and x, y are unknowns.
Pell equation: x^2 - Dy^2 = 1 (or = -1, or = N) where D is a positive non-square integer.
Pythagorean: x^2 + y^2 = z^2.
General quadratic: ax^2 + bxy + cy^2 + dx + ey + f = 0.
Higher-order or special: Fermat-type (x^n + y^n = z^n for n > 2), sum of squares, or other.
Record the classification and chosen method.
Expected: A precise classification with the solving strategy identified.
On failure: If the equation does not fit a standard type, try substitution or transformation to reduce it to a known form. For example, x^2 + y^2 + z^2 = n can be approached via Legendre's three-square theorem. If no reduction is apparent, apply modular constraints (Step 4) to test for obstructions.
Solve ax + by = c for integer x, y.
Compute g = gcd(a, b) using the Euclidean algorithm.
Test solvability: Solutions exist if and only if g | c.
Simplify: Divide through by g to get (a/g)x + (b/g)y = c/g, where now gcd(a/g, b/g) = 1.
Find a particular solution using the extended Euclidean algorithm:
Write the general solution:
Apply constraints (if positive solutions required):
Example (15x + 21y = 39):
gcd(15, 21) = 3. Does 3 | 39? Yes.
Simplify: 5x + 7y = 13.
Extended Euclidean: 1 = 3*5 - 2*7.
Multiply by 13: 13 = 39*5 - 26*7.
Particular: x0 = 39, y0 = -26.
General: x = 39 + 7k, y = -26 - 5k, k in Z.
Check (k=0): 5*39 + 7*(-26) = 195 - 182 = 13. Correct.
Expected: The general solution family (x, y) parameterized by an integer k, with verification of the particular solution.
On failure: If the particular solution is wrong, re-check the extended Euclidean back-substitution step by step. The most common error is a sign mistake. Verify: a * x0 + b * y0 should equal c exactly (not just modulo something).
Solve x^2 - Dy^2 = 1 where D is a positive non-square integer.
Verify D is not a perfect square: If D = k^2, then x^2 - k^2*y^2 = (x - ky)(x + ky) = 1, which forces x - ky = x + ky = +/-1, giving y = 0, x = +/-1 (trivial). The equation is interesting only for non-square D.
Compute the continued fraction expansion of sqrt(D):
Extract the fundamental solution from convergents:
Generate further solutions from the fundamental solution (x1, y1):
Present the fundamental solution and the recurrence for generating all solutions.
Fundamental solutions for small D:
| D | (x1, y1) | D | (x1, y1) | D | (x1, y1) |
|---|---|---|---|---|---|
| 2 | (3, 2) | 7 | (8, 3) | 13 | (649, 180) |
| 3 | (2, 1) | 8 | (3, 1) | 14 | (15, 4) |
| 5 | (9, 4) | 10 | (19, 6) | 15 | (4, 1) |
| 6 | (5, 2) | 11 | (10, 3) | 17 | (33, 8) |
Expected: The fundamental solution (x1, y1) verified by substitution, plus the recurrence for generating all positive solutions.
On failure: If the continued fraction computation does not converge to a period, check the iteration formula. The period length r can be large (e.g., D = 61 has r = 11 and fundamental solution (1766319049, 226153980)). For large D, use computational tools rather than manual computation.
Prove that an equation has no integer solutions by showing a modular obstruction.
Choose a modulus m (typically m = 2, 3, 4, 5, 7, 8, or 16).
Enumerate all residues: Compute the left-hand side modulo m for all possible residues of the variables.
Check if any combination gives the required right-hand side modulo m.
Common obstructions:
If no obstruction is found, a modular approach cannot prove non-existence. Solutions may or may not exist; try constructive methods or descent.
Quadratic residues reference:
| Mod | Squares (residues) |
|---|---|
| 3 | {0, 1} |
| 4 | {0, 1} |
| 5 | {0, 1, 4} |
| 7 | {0, 1, 2, 4} |
| 8 | {0, 1, 4} |
| 11 | {0, 1, 3, 4, 5, 9} |
| 13 | {0, 1, 3, 4, 9, 10, 12} |
| 16 | {0, 1, 4, 9} |
Expected: Either a proof of non-existence via modular obstruction, or a statement that no obstruction was found at the tested moduli.
On failure: If modular methods are inconclusive, try infinite descent: assume a solution exists, derive a strictly smaller solution, and repeat until a contradiction with positivity is reached. This technique is classical for proving x^4 + y^4 = z^2 has no non-trivial solutions.
Express all solutions in terms of the fundamental solution and integer parameters.
For linear equations: The family is x = x0 + (b/g)*k, y = y0 - (a/g)*k (from Step 2).
For Pell equations: Use the recurrence from Step 3 to generate the first several solutions:
(x1, y1), (x2, y2), (x3, y3), ...
List at least 3-5 solutions as a sanity check.
For Pythagorean triples: Generate primitive triples from parameters m > n > 0, gcd(m, n) = 1, m - n odd:
For general families: Express solutions in parametric form if possible. If the equation defines a curve of genus 0, a rational parametrization exists. If genus >= 1, there may be finitely many solutions (Faltings' theorem for genus >= 2).
Verify at least 3 members of the family by substitution into the original equation.
Example (Pell, D = 2):
Fundamental: (x1, y1) = (3, 2). Check: 9 - 2*4 = 1. Correct.
(x2, y2) = (3*3 + 2*2*2, 3*2 + 2*3) = (17, 12). Check: 289 - 2*144 = 1.
(x3, y3) = (3*17 + 2*2*12, 3*12 + 2*17) = (99, 70). Check: 9801 - 2*4900 = 1.
Expected: A parametric or recursive description of all solutions, with at least 3 solutions verified.
On failure: If generated solutions fail verification, the fundamental solution or the recurrence formula is wrong. For Pell equations, re-derive the fundamental solution from the continued fraction. For linear equations, re-check the extended Euclidean computation.
Assuming all equations with gcd | c have positive solutions: The general solution x = x0 + (b/g)*k includes negative values. Positive solutions may not exist even when the equation is solvable over all integers.
Confusing x^2 - Dy^2 = 1 with x^2 - Dy^2 = -1: The negative Pell equation has solutions only when the continued fraction period length is odd. Applying the positive equation formula to a negative equation target gives the wrong result.
Forgetting the trivial solution of Pell's equation: (x, y) = (1, 0) always satisfies x^2 - Dy^2 = 1 but is not useful for generating non-trivial solutions. The fundamental solution is the smallest solution with y > 0.
Incomplete modular obstruction: Checking only mod 2 or mod 4 may miss obstructions visible at higher moduli. If the first few moduli show no obstruction, try mod 8, 9, 16, or the discriminant of the quadratic form.
Off-by-one in continued fraction period: The convergent indices must be carefully tracked. The fundamental solution comes from p_{r-1}/q_{r-1} where r is the period length, not from p_r/q_r.
Infinite descent without a base case: When using descent to prove non-existence, you must show that the descent terminates at a contradiction (e.g., x = 0 contradicts x > 0). Without this base case, the argument is incomplete.
Applying Fermat's Last Theorem incorrectly: x^n + y^n = z^n has no non-trivial integer solutions for n > 2 (Wiles, 1995), but this does not apply to equations with different coefficients like 2x^3 + 3y^3 = z^3.
analyze-prime-numbers -- Factorization and gcd computation are prerequisites for Diophantine solvingsolve-modular-arithmetic -- Linear congruences ax = c (mod b) are equivalent to linear Diophantine equationsderive-theoretical-result -- Formal derivation techniques for proving Diophantine impossibility results