MathLabs

Arithmetic and number theory

Pell's equation

The equation x2−dy2=1x^2-dy^2=1, whose integer solutions connect to continued fractions.

IntuitionIntuition: integer points on a hyperbola that approximate d\sqrt{d}

Look for integers x,yx,y with x2−2y2=1x^2-2y^2=1. Trying small values: (x,y)=(3,2)(x,y)=(3,2) works, since 9−8=19-8=1. Rearranging, (xy)2=2+1y2\left(\frac{x}{y}\right)^2=2+\frac{1}{y^2}, so x/y=3/2=1.5x/y=3/2=1.5 is already a startlingly good approximation to 2≈1.41421\sqrt{2}\approx 1.41421. Equations of the form x2−dy2=1x^2 - d y^2 = 1 — called Pell's equation — turn out to be inseparable from the theory of continued fractions and best rational approximations.

A path graph of continued fraction convergents with one node highlighted as the fundamental solution of the Pell equation.
The continued-fraction convergents of d\sqrt{d} drawn as a path graph: each node is a convergent pk/qkp_k/q_k, and the highlighted node is the fundamental solution.

AdvancedDefinition, fundamental solution, and structure

Definition: Pell's equation and fundamental solution

For a positive integer dd that is not a perfect square, x2−dy2=1x^2 - d y^2 = 1 is called Pell's equation. Among its positive integer solutions (x,y)(x,y), the one with smallest xx (equivalently smallest yy) is the fundamental solution (x1,y1)(x_1,y_1).

x2−dy2=1,d∈Z>0 not a perfect squarex^2 - d y^2 = 1,\qquad d\in\mathbb{Z}_{>0}\ \text{not a perfect square}

Factoring the left side as (x−yd)(x+yd)=1(x-y\sqrt d)(x+y\sqrt d)=1 turns Pell's equation into a statement about the ring Z[d]\mathbb{Z}[\sqrt d]: solutions correspond to elements of norm 11, and multiplying two such elements gives another. In particular, once we know (x1,y1)(x_1,y_1), every further solution is obtained by taking powers: xn+ynd=(x1+y1d)nx_n+y_n\sqrt{d} = (x_1+y_1\sqrt{d})^n.

(x1+y1d)(x1−y1d)=1 ⟹ xn+ynd=(x1+y1d)n(x_1+y_1\sqrt d)(x_1-y_1\sqrt d)=1 \ \Longrightarrow\ x_n+y_n\sqrt d = (x_1+y_1\sqrt d)^n
Fundamental solutions for small dd
ddContinued fraction of d\sqrt dFundamental solution (x1,y1)(x_1,y_1)
22[1;2‾][1;\overline{2}](3,2)(3,2)
33[1;1,2‾][1;\overline{1,2}](2,1)(2,1)
55[2;4‾][2;\overline{4}](9,4)(9,4)
77[2;1,1,1,4‾][2;\overline{1,1,1,4}](8,3)(8,3)

AdvancedTheorems and proofs

For every positive integer dd that is not a perfect square, x2−dy2=1x^2 - d y^2 = 1 has a solution with x,yx,y positive integers.

Why is it true?

It is not obvious at all that a hyperbola x2−dy2=1x^2-dy^2=1, which certainly has real points, must pass through a lattice point — this theorem guarantees it always does, for every non-square dd, via a clever pigeonhole argument on rational approximations.

Proof

By Dirichlet's approximation theorem, for any integer Q>0Q>0 there exist integers p,qp,q with 1≤q≤Q1\le q\le Q and ∣d−pq∣<1qQ\left|\sqrt d - \frac{p}{q}\right|<\frac{1}{qQ}. Letting Q→∞Q\to\infty produces infinitely many pairs (p,q)(p,q) with ∣d−pq∣<1q2\left|\sqrt d-\frac{p}{q}\right|<\frac{1}{q^2}.

For each such pair, ∣p−qd∣<1q|p-q\sqrt d|<\frac{1}{q}, so ∣p2−dq2∣=∣p−qd∣⋅∣p+qd∣<1q(2qd+1q)<2d+1|p^2-dq^2|=|p-q\sqrt d|\cdot|p+q\sqrt d|<\frac{1}{q}\left(2q\sqrt d+\frac{1}{q}\right)<2\sqrt d+1. Thus p2−dq2p^2-dq^2 takes one of only finitely many integer values in (−2d−1, 2d+1)(-2\sqrt d-1,\,2\sqrt d+1), while there are infinitely many pairs (p,q)(p,q).

By the pigeonhole principle, some fixed nonzero integer kk in that finite range satisfies p2−dq2=kp^2-dq^2=k for infinitely many pairs (p,q)(p,q). Among these infinitely many pairs, again by pigeonhole, infinitely many share the same residues p≡p0, q≡q0(mod∣k∣)p\equiv p_0,\ q\equiv q_0\pmod{|k|}.

Take two distinct such pairs (p1,q1)≠(p2,q2)(p_1,q_1)\neq(p_2,q_2) with p12−dq12=p22−dq22=kp_1^2-dq_1^2=p_2^2-dq_2^2=k and matching residues mod ∣k∣|k|. Set x+yd=(p1+q1d)(p2−q2d)kx+y\sqrt d = \frac{(p_1+q_1\sqrt d)(p_2-q_2\sqrt d)}{k}; expanding shows x=p1p2−dq1q2kx=\frac{p_1p_2-dq_1q_2}{k} and y=p1q2−p2q1ky=\frac{p_1q_2-p_2q_1}{k} are honest integers precisely because of the matching residues mod ∣k∣|k|, and multiplicativity of the norm N(a+bd)=a2−db2N(a+b\sqrt d)=a^2-db^2 gives x2−dy2=k⋅kk2=1x^2-dy^2=\frac{k\cdot k}{k^2}=1. Since (p1,q1)≠(p2,q2)(p_1,q_1)\neq(p_2,q_2) but they give the same ratio in the limit, one checks y≠0y\neq 0, and replacing (x,y)(x,y) by (∣x∣,∣y∣)(|x|,|y|) (still a solution, since only squares appear) gives a solution in positive integers.

If (x1,y1)(x_1,y_1) is the fundamental solution of x2−dy2=1x^2 - d y^2 = 1, then every solution in positive integers (x,y)(x,y) equals (xn,yn)(x_n,y_n) for some n≥1n\ge 1, where xn+ynd=(x1+y1d)nx_n+y_n\sqrt{d} = (x_1+y_1\sqrt{d})^n.

Why is it true?

It says the infinitely many solutions are not a mysterious scattered set but a completely predictable geometric-like sequence generated by repeatedly "multiplying" the smallest one — reducing an infinite search to finding just one number.

Proof

First check (xn,yn)(x_n,y_n) defined by xn+ynd=(x1+y1d)nx_n+y_n\sqrt d=(x_1+y_1\sqrt d)^n is indeed a solution for every nn: taking conjugates, xn−ynd=(x1−y1d)nx_n-y_n\sqrt d=(x_1-y_1\sqrt d)^n, so xn2−dyn2=(xn+ynd)(xn−ynd)=[(x1+y1d)(x1−y1d)]n=(x12−dy12)n=1n=1x_n^2-dy_n^2=(x_n+y_n\sqrt d)(x_n-y_n\sqrt d)=\left[(x_1+y_1\sqrt d)(x_1-y_1\sqrt d)\right]^n=(x_1^2-dy_1^2)^n=1^n=1.

Now suppose (x,y)(x,y) is any positive integer solution not of this form; since xn→∞x_n\to\infty as n→∞n\to\infty, there is a unique nn with xn+ynd≤x+yd<xn+1+yn+1d=(xn+ynd)(x1+y1d)x_n+y_n\sqrt d \le x+y\sqrt d < x_{n+1}+y_{n+1}\sqrt d = (x_n+y_n\sqrt d)(x_1+y_1\sqrt d).

Divide through by (xn+ynd)(x_n+y_n\sqrt d), i.e. multiply by its inverse (xn−ynd)(x_n-y_n\sqrt d) (valid since xn2−dyn2=1x_n^2-dy_n^2=1): set x′+y′d=(x+yd)(xn−ynd)x'+y'\sqrt d = (x+y\sqrt d)(x_n-y_n\sqrt d). Then 1≤x′+y′d<x1+y1d1\le x'+y'\sqrt d < x_1+y_1\sqrt d, and x′2−dy′2=(x2−dy2)(xn2−dyn2)=1⋅1=1x'^2-dy'^2=(x^2-dy^2)(x_n^2-dy_n^2)=1\cdot 1=1, so (x′,y′)(x',y') is also a solution of Pell's equation.

A short computation using x′+y′d≥1x'+y'\sqrt d\ge 1 and x′2−dy′2=1x'^2-dy'^2=1 shows x′≥1x'\ge 1 and y′≥0y'\ge 0 (a solution with x′+y′d≥1x'+y'\sqrt d\ge 1 but y′<0y'<0 would force x′>x1x'>x_1, contradicting x′+y′d<x1+y1dx'+y'\sqrt d<x_1+y_1\sqrt d combined with the norm equation). If y′>0y'>0, then (x′,y′)(x',y') is a positive solution with x′+y′d<x1+y1dx'+y'\sqrt d<x_1+y_1\sqrt d, contradicting minimality of the fundamental solution. So y′=0y'=0, forcing x′=1x'=1, i.e. x+yd=xn+yndx+y\sqrt d=x_n+y_n\sqrt d, so (x,y)=(xn,yn)(x,y)=(x_n,y_n) after all — contradicting our assumption.

Hence every positive solution is exactly some (xn,yn)(x_n,y_n), proving the fundamental solution generates the entire solution set.

AdvancedReal-World Applications and Worked Examples

Pell's equation is far more than a puzzle: it governs the best rational approximations used in mechanical gear design, its arithmetic underlies classical integer-factorization algorithms, and computing its fundamental solution for large dd is exactly the computational problem that Hallgren's quantum algorithm solves in polynomial time — with no known efficient classical method.

Example: Finding the fundamental solution for d=2d=2

Use the continued fraction of 2\sqrt{2} to find the fundamental solution of x2−2y2=1x^2-2y^2=1, then generate the next solution.

Solution

The continued fraction of 2\sqrt2 is [1;2‾]=1+12+12+⋯[1;\overline{2}]=1+\cfrac{1}{2+\cfrac{1}{2+\cdots}}, with convergents 1, 32, 75, 1712, 4129,…1,\ \frac{3}{2},\ \frac{7}{5},\ \frac{17}{12},\ \frac{41}{29},\dots

Checking the convergent 32\frac{3}{2}: 32−2⋅22=9−8=13^2-2\cdot 2^2=9-8=1. This is the smallest convergent that works, so the fundamental solution is (x1,y1)=(3,2)(x_1,y_1)=(3,2).

Using the recursion xn+ynd=(x1+y1d)nx_n+y_n\sqrt{d} = (x_1+y_1\sqrt{d})^n with n=2n=2: x2+y22=(3+22)2=9+122+8=17+122x_2+y_2\sqrt2=(3+2\sqrt2)^2=9+12\sqrt2+8=17+12\sqrt2, so (x2,y2)=(17,12)(x_2,y_2)=(17,12).

Verify: 172−2⋅122=289−288=117^2-2\cdot 12^2=289-288=1, confirming the next solution, which matches the convergent 1712\frac{17}{12} found above — exactly as the theory predicts.

Example: Engineering approximation: designing a gear ratio close to 2\sqrt{2}

A mechanism needs two meshed gears whose tooth-count ratio approximates 2\sqrt2 as closely as possible using small integer tooth counts, so the error stays tiny after many rotations. Use the Pell equation convergents to choose the tooth counts and bound the error.

Solution

From the fundamental solution (3,2)(3,2) of x2−2y2=1x^2-2y^2=1, the ratio 3/2=1.53/2=1.5 gives ∣3/2−2∣≈0.0858|3/2-\sqrt2|\approx 0.0858 — usable, but coarse for precision machinery.

Using the next convergent from (17,12)(17,12) (found by squaring 3+223+2\sqrt2): a gear pair with 1717 and 1212 teeth gives ratio 17/12≈1.4166717/12\approx 1.41667, and ∣17/12−2∣≈0.00245|17/12-\sqrt2|\approx 0.00245, nearly 3535 times more accurate than the 3/23/2 design for only modestly larger tooth counts.

The general bound for any convergent pk/qkp_k/q_k of a continued fraction is ∣pkqk−2∣<1qk2\left|\frac{p_k}{q_k}-\sqrt2\right|<\frac{1}{q_k^2}, so as engineers move to the next Pell solution (x3,y3)(x_3,y_3) (obtained from (3+22)3=99+702(3+2\sqrt2)^3=99+70\sqrt2, i.e. 99/7099/70) the tooth count grows to 7070 but the error shrinks below 1/702≈0.00021/70^2\approx 0.0002.

This illustrates the fundamental engineering trade-off explicit in Pell's equation: each successive solution (xn,yn)(x_n,y_n) trades a manufacturing cost increase (more teeth, i.e. larger yny_n) for a quantifiable, rapidly shrinking approximation error, letting a designer pick exactly the point on this curve that fits a budget and precision requirement.

What is the fundamental solution of x2−2y2=1x^2-2y^2=1?

If (x1,y1)(x_1,y_1) is the fundamental solution, how is the solution (x2,y2)(x_2,y_2) obtained?

Does x2−3y2=−1x^2-3y^2=-1 have an integer solution?

Hallgren's algorithm solves Pell's equation in polynomial time using what kind of computation?

References

  1. Sean Hallgren (2007). Polynomial-time quantum algorithms for Pell's equation and the principal ideal problem · DOI:10.1145/1206035.1206039
  2. Hendrik W. Lenstra Jr. (2002). Solving the Pell Equation