Pell's Equation and Its Surprisingly Large Solutions
Solve x² − Ny² = 1 with continued fractions and composition, and see why some small values of N force enormous first solutions.
Key takeaways
- For every positive nonsquare N, the equation x² − Ny² = 1 has infinitely many solutions in positive integers.
- All of them are generated from the smallest one by a single multiplication rule.
- The continued fraction of √N finds the smallest solution systematically.
- The size of that first solution is very irregular and can be huge even when N is small.
- Solutions give extremely accurate rational approximations to √N.
Pell's equation is x² − Ny² = 1, where N is a positive integer that is not a perfect square, and you want x and y to be positive integers. It always has infinitely many solutions. The surprise is how large the smallest one can be, and how wildly its size jumps from one value of N to the next.
For N = 60 the smallest solution is x = 31, y = 4. For N = 61 it is x = 1766319049, y = 226153980. This article explains where such numbers come from and how to find them without a blind search.
What the equation asks
Rewrite the equation as x² = Ny² + 1. You are looking for squares that exceed N times another square by exactly 1. Divide by y² and you get (x/y)² = N + 1/y², so x/y is very close to √N.
Every N has the trivial solution x = 1, y = 0. The question is about solutions with y ≥ 1. If N were a perfect square, say d², the left side would factor as (x − dy)(x + dy) = 1, leaving only the trivial solutions. That is why squares are excluded.
For small N a table of values works. Try N = 7 and compute 7y² + 1 for y = 1, 2, 3: you get 8, 29 and 64. The third is a square, so x = 8, y = 3.
A picture of the problem
In the plane, x² − Ny² = 1 is a hyperbola passing through (1, 0). Its two branches approach straight lines of slope 1/√N. Those lines have irrational slope, so they pass through no grid point except the origin.
Solving the equation means finding grid points that land exactly on the curve. Far from the origin the curve hugs its guide line very closely, and a grid point on it must be one that nearly lies on a line it can never touch. This is why the question is really about how well fractions can approximate an irrational number.
It also explains why searching is a poor strategy. Testing y = 1, 2, 3 and onward works instantly for N = 7. For N = 61 you would need to test more than two hundred million values of y before the first success.
One solution gives them all
The key identity, known to the Indian mathematician Brahmagupta in the seventh century, is
(a² − Nb²)(c² − Nd²) = (ac + Nbd)² − N(ad + bc)² It says that combining two solutions of the equation gives another. Think of a solution as the number x + y√N. Combining solutions is simply multiplying these numbers.
For N = 2 the smallest solution is (3, 2). Powers of 3 + 2√2 give
| power | x | y | check |
|---|---|---|---|
| 1 | 3 | 2 | 9 − 8 = 1 |
| 2 | 17 | 12 | 289 − 288 = 1 |
| 3 | 99 | 70 | 9801 − 9800 = 1 |
| 4 | 577 | 408 | 332929 − 332928 = 1 |
Every positive solution appears in this list. The proof divides a supposed extra solution by powers of the smallest one until it lands strictly between 1 and the smallest, which is impossible. The solutions grow by a roughly constant factor, here about 5.83 each time.
Finding the smallest solution
The reliable method uses continued fractions. Write √N as an integer part plus 1 divided by something, then repeat on the something. For a nonsquare N the terms eventually repeat in a cycle.
For √7 the expansion is 2; 1, 1, 1, 4, followed by 1, 1, 1, 4 forever. Cutting the expansion off after more and more terms gives the convergents.
| convergent p/q | p² − 7q² |
|---|---|
| 2/1 | −3 |
| 3/1 | 2 |
| 5/2 | −3 |
| 8/3 | 1 |
The fourth one solves the equation. This is no accident: the solutions of Pell's equation are always found among the convergents of √N. Lagrange proved in the eighteenth century that the method succeeds for every nonsquare N.
Computing the terms by hand
You can expand √N using only integers. Let a₀ be the integer part of √N, and start with m = 0, d = 1 and a = a₀. Then repeat:
new m = d·a − m
new d = (N − (new m)²) / d always divides exactly
new a = integer part of (a₀ + new m) / new d For N = 7, a₀ = 2. The successive values of (m, d, a) are (2, 3, 1), (1, 2, 1), (1, 3, 1) and (2, 1, 4). The value d = 1 signals the end of the repeating block.
Convergents follow their own rule: each new numerator is a times the previous numerator plus the one before that, and denominators obey the same recipe. With terms 2, 1, 1, 1, the numerators are 2, 3, 5, 8 and the denominators 1, 1, 2, 3. No decimal approximation of √7 is needed at any point, so rounding error never enters.
The negative equation
The companion equation x² − Ny² = −1 is solvable for some N and not others. It has a solution exactly when the repeating block of √N has odd length.
For N = 13 the block has length 5, and a convergent gives 18² − 13 × 5² = 324 − 325 = −1. Squaring 18 + 5√13 gives 649 + 180√13, and 649, 180 is the smallest solution of the positive equation. This shortcut halves the digits you need to reach.
For N = 7 no solution to the negative equation exists, which you can see by working modulo 7: it would require x² ≡ 6, and 6 is not a square there. In general, any prime factor of N of the form 4k + 3 rules it out, an application of quadratic residues.
How large can the first solution be?
Neighbors can differ enormously.
| N | smallest x | smallest y |
|---|---|---|
| 10 | 19 | 6 |
| 29 | 9801 | 1820 |
| 46 | 24335 | 3588 |
| 53 | 66249 | 9100 |
| 61 | 1766319049 | 226153980 |
| 62 | 63 | 8 |
| 109 | 158070671986249 | 15140424455100 |
The size is tied to the length of the repeating block of √N. Values just next to a square, such as 62 or 63, have very short blocks and tiny solutions. Others have long blocks, and each additional term multiplies the size of the convergents. No simple formula predicts the length from N, and understanding its typical behavior leads into deep algebraic number theory.
Composition at work on N = 61
Brahmagupta's identity also helps when you have only a near miss. Notice that 39² − 61 × 5² = 1521 − 1525 = −4. This is not a solution, but its right side is small. Dividing 39 and 5 by 2 gives a solution of x² − 61y² = −1 in halves: x = 39/2 and y = 5/2.
Cubing the number (39 + 5√61)/2 removes the fractions and produces 29718 + 3805√61, which satisfies x² − 61y² = −1. Squaring that gives 1766319049 + 226153980√61, the smallest solution with right side 1. Two multiplications turn a pair of two-digit numbers into the famous giant.
A history with a misleading name
Brahmagupta studied the equation and used his composition rule to solve difficult cases. Later Indian mathematicians, notably Bhaskara II in the twelfth century, described a cyclic method called chakravala that handles N = 61 with remarkably little work.
In 1657 Pierre de Fermat posed cases including N = 61 as a challenge to other mathematicians. English mathematicians, including William Brouncker, found a general method. Euler later attributed it to John Pell, apparently by mistake, and the name stuck.
An older puzzle attributed to Archimedes, the cattle problem, asks for the sizes of herds subject to a list of conditions. In its full form it reduces to a Pell equation, and the smallest answer has more than 200,000 digits, far beyond anything that could have been written out in antiquity.
Where the solutions are useful
Each solution gives a superb approximation to √N. From (577, 408) you get 577/408 = 1.4142156..., which agrees with √2 = 1.4142135... to five decimal places. The error is roughly 1/(2y²√N), so doubling the number of digits in y roughly doubles the number of correct digits.
Pell equations also answer counting questions. Which triangular numbers are squares? Setting n(n + 1)/2 = m² and rearranging gives (2n + 1)² − 8m² = 1. The solutions give the squares 1, 36, 1225 and so on, linking to figurate numbers.
In the language of number systems, the numbers x + y√N with x² − Ny² = ±1 are units: they have reciprocals of the same form.
Try it yourself
- For N = 3, find the smallest solution by trial. You should reach (2, 1), then (7, 4) by squaring 2 + √3.
- For N = 5, check that (9, 4) works and that (2, 1) solves the negative equation.
- For N = 11, test 11y² + 1 for small y. The first square appears at y = 3, giving (10, 3).
If a hand search seems to go on forever, do not conclude that nothing exists. For this equation a solution is guaranteed, and the continued fraction will reach it. This is a useful contrast with other Diophantine equations, where a long fruitless search may or may not mean something.
Beyond the right side of 1
You can also ask about x² − Ny² = c for other integers c. Here solutions need not exist. When one does exist, multiplying it by solutions of the equation with right side 1 produces infinitely many more, by the same identity. So the solutions fall into a finite number of families, and the work lies in finding one small starting member of each. Remainder tests are again the first thing to try when you suspect there is none.