Pell's Equation
Pell's equation is the deceptively simple Diophantine equation
x^2 - D\,y^2 = 1,
for a non-square positive integer D, sought in whole numbers
x, y. At a glance it looks tame — maybe a handful of small solutions, if
any. In reality it has infinitely many solutions for every valid D,
and the very smallest one can already involve numbers with seven digits or more. This one equation was
studied seriously by ancient Indian and Greek mathematicians well over a thousand years before anyone
named Pell was born — and the name that stuck to it is, famously, a historical mix-up.
Why should anyone care about a single equation with two unknowns? Because it turns out to be the key
that unlocks the structure of an entire family of number systems — and because chasing down its
solutions by hand, long before calculators, was itself a serious mathematical sport for over a thousand
years across three continents.
Worked example: finding the smallest solution for D = 2
Take the simplest interesting case, D = 2:
x^2 - 2y^2 = 1.
The most direct approach is plain trial: try y = 1, 2, 3, \ldots and check
whether 2y^2 + 1 happens to be a perfect square.
- y = 1: 2(1)^2 + 1 = 3 — not a square.
- y = 2: 2(2)^2 + 1 = 9 = 3^2 — a square! So
x = 3.
Check it: 3^2 - 2(2)^2 = 9 - 8 = 1. It works. The pair
(x, y) = (3, 2) is the fundamental solution — the smallest
nontrivial one — for D = 2. Run the same trial search yourself below.
// Trial search for the smallest solution of x^2 - D*y^2 = 1.
function fundamentalSolution(D: number, maxY: number): [number, number] | null {
for (let y = 1; y <= maxY; y++) {
const rhs = D * y * y + 1;
const x = Math.round(Math.sqrt(rhs));
if (x * x === rhs) return [x, y];
}
return null;
}
const D = 2;
const solution = fundamentalSolution(D, 100);
if (solution) {
const [x, y] = solution;
console.log(`Smallest solution for D = ${D}: x = ${x}, y = ${y}`);
console.log(`Check: ${x}^2 - ${D}*${y}^2 = ${x * x - D * y * y}`);
}
One solution gives them all
Once you have the fundamental solution (x_1, y_1), you never need to search
by trial again — every other solution is generated automatically by taking powers in the number system
\mathbb{Z}[\sqrt D]:
x_k + y_k\sqrt{D} = \left(x_1 + y_1\sqrt{D}\right)^{k}, \qquad k = 1, 2, 3, \ldots
For D = 2, squaring 3 + 2\sqrt 2 gives
(3 + 2\sqrt2)^2 = 9 + 12\sqrt2 + 8 = 17 + 12\sqrt2,
so the next solution is (17, 12). Check it:
17^2 - 2(12)^2 = 289 - 288 = 1. Cubing gives
(99, 70), and the solutions keep marching off to infinity, one power at a
time, each one bigger than the last.
Finding the first solution systematically
Trial and error works fine for D = 2, but it becomes hopeless for larger
D. The systematic route is through the periodic
continued fraction
of \sqrt{D}. For D = 2,
\sqrt{2} = [1; \overline{2}] = 1 + \cfrac{1}{2 + \cfrac{1}{2 + \cfrac{1}{2 + \cdots}}},
a purely periodic pattern after the first term. The convergent just before the pattern completes one
full period — here, 1 + \tfrac{1}{2} = \tfrac{3}{2} — delivers the
fundamental solution directly: numerator and denominator are exactly x_1 = 3
and y_1 = 2, matching the trial search above. For every non-square
D, this method is guaranteed to terminate and always finds the fundamental
solution — no guessing required, however large D is.
A bonus: astonishingly good square-root approximations
Rearranging x^2 - Dy^2 = 1 as x^2 = Dy^2 + 1
shows that x/y must be very close to \sqrt{D}
whenever y is reasonably large — the "+1" barely matters once
Dy^2 is big. Pell's solutions, it turns out, are exactly the best possible
whole-number fraction approximations to \sqrt{D} for their size, which is
no coincidence: they are the very continued-fraction convergents used to find them.
Watch the fractions from the D = 2 solutions above race towards
\sqrt{2} \approx 1.41421356\ldots:
- 3/2 = 1.5
- 17/12 = 1.41\overline{6}
- 99/70 = 1.4142857\ldots
Each successive Pell solution roughly squares the accuracy of the last — a handful of
digits in x and y buys many more correct digits
of \sqrt{D} than you might expect. Before electronic calculators existed,
this was a genuinely practical way to compute square roots to high precision by hand.
When the numbers explode
The continued-fraction method is essential because brute-force search is not merely slow for some
values of D — it is completely hopeless. For D = 61,
an innocent-looking case, the smallest solution is already
x_1 = 1{,}766{,}319{,}049, \qquad y_1 = 226{,}153{,}980.
Trying every y from 1 up to
226{,}153{,}980 by hand is obviously never going to happen; even a modern
computer doing blind trial would grind for a long time. Only the continued-fraction shortcut makes
this tractable at all — see the vignette below for why Fermat chose this exact case to embarrass his
rivals.
Why it matters
Pell's equation is the prototype for understanding units in
rings of algebraic integers:
its solutions are the units of \mathbb{Z}[\sqrt D] (the elements
with a multiplicative inverse in that ring), and the fact that they form an infinite family generated
by repeated powers of one fundamental unit is a special case of Dirichlet's much deeper unit theorem
for general number fields. A two-thousand-year-old puzzle about squares and non-squares turns out to be
a perfect first doorway into the structure of modern algebraic number theory.
There's also a tidy theoretical reason the method above always works: a theorem of Lagrange guarantees
that the continued fraction of any irrational square root \sqrt{D}
is eventually periodic — never a one-off jumble of digits, always a pattern that repeats forever once
it settles in. That guaranteed periodicity is exactly what makes the continued-fraction method reliable
for every non-square D whatsoever, not just tidy small cases like
D=2.
The name is a genuine historical accident. The English mathematician John Pell
(1611–1685) did essentially no original work on this equation. Leonhard Euler mistakenly attributed
results on it to Pell — apparently confusing his work with that of Pell's countryman William Brouncker,
who really had worked on it — and the mistaken name stuck permanently once Euler's hugely influential
textbooks were in print.
The equation was solved, in essence, centuries earlier and half a world away. The Indian mathematician
Brahmagupta (7th century CE) discovered a clever composition technique for combining
near-solutions into better ones, and Bhāskara II (12th century CE) completed it into a
full algorithm called the chakravala (or "cyclic") method — a systematic, general procedure
for finding the fundamental solution for any D, well before Fermat or Pell
were born. Some historians rate the chakravala method as one of the finest achievements of pre-modern
number theory anywhere in the world. "Pell's equation" might be the most successful mathematical
misnomer in history.
In 1657, Pierre de Fermat — yes, the same Fermat from the margin note — sent a deliberately nasty
challenge to English mathematicians, including John Wallis and William Brouncker: find whole-number
solutions to x^2 - 61y^2 = 1 and x^2 - 109y^2 = 1.
Fermat almost certainly already knew, or strongly suspected, that these particular values of
D produce fundamental solutions with unusually huge digit counts — he picked
them precisely because they were nasty, as a kind of mathematical prank aimed at rival
mathematicians across the Channel.
Brouncker and Wallis did eventually work out a general method (independently rediscovering something
close to the chakravala algorithm) and solved both cases correctly. It remains a wonderful illustration
of just how deceptive Pell's equation can be: the equation itself is four symbols long, yet for the
"wrong" choice of D its answer can run to seven digits or more, with
absolutely no visible warning from the equation itself that this is coming.
The "ancient Greek mathematicians" mentioned above include Archimedes himself. In a poem-riddle he
supposedly sent to Eratosthenes, Archimedes describes a herd of cattle sacred to the sun god Helios,
split into bulls and cows of four colours, and gives a tangle of ratio conditions between them — plus
two extra conditions requiring certain totals to be a perfect square and a triangular number. Untangle
all of the algebra, and the whole riddle reduces to a single Pell equation with
D = 410{,}286{,}423{,}278{,}424.
The smallest solution — the actual number of cattle in Archimedes's riddle — has been calculated to
have roughly 206,545 digits. No one could find it by hand; it was finally computed in
full using a computer in 1965. It is one of the earliest known real-world appearances of a Pell
equation, and almost certainly the most extreme "innocent-looking equation, monstrous answer" in the
entire history of mathematics.