Pell equations
x² − Dy² = N. For D > 0 not a square the solutions come in finitely many families, each a fundamental solution multiplied over and over by the fundamental unit. Finding every family takes only a bounded search.
Families of solutions
Write , with norm . Norms multiply, so if solves and is the smallest solution of , then every is again a solution. Each class of solutions is one such orbit (up to sign), and there are finitely many classes.
Nagell's bounds say each class has a member with
so a search over that range of finds every class. pellSolutions returns those fundamental solutions, the unit, and a solutions(count) that walks the orbits in increasing size.
import { pellSolutions } from "@ratmath/numtheory";
const r = pellSolutions(2, 7);
r.kind; // "infinite"
r.unit; // [3n, 2n]: 3² − 2·2² = 1
r.fundamental; // [[3n, 1n], [3n, -1n]]: two classes
r.solutions(5); // [[3n, 1n], [5n, 3n], [13n, 9n], [27n, 19n], [75n, 53n]]
pellSolutions(61, 1).unit; // [1766319049n, 226153980n]
pellSolutions(13, -1).fundamental; // [[18n, 5n]]
pellSolutions(3, 2).fundamental; // []: x² − 3y² = 2 has no solutions (try mod 3)
When there are finitely many
For the form is positive definite, and for a perfect square it factors as , so in both cases there are finitely many solutions and they are all listed.
pellSolutions(-1, 25).solutions.length; // 12: the points of x² + y² = 25
pellSolutions(4, 5).solutions; // [[-3n, -1n], [-3n, 1n], [3n, -1n], [3n, 1n]]
The same question in the language of number fields is the norm equation in the order ; normEquation is a second name for it. The general theory, with class groups, is on the quadratic forms page.
@ratmath/numtheory and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math.