0% found this document useful (0 votes)
162 views39 pages

Understanding Pell's Equations

This paper investigates Pell Equations, which are equations of the form x² - dy² = k, focusing on integer solutions for various values of k. The author provides proofs for the solvability of the equation when k = 1 and explores the classification of Pell Equations as solvable or unsolvable for other values of k, particularly k = 4. Additionally, the paper discusses patterns in solutions and the use of computers to find these solutions.

Uploaded by

vicofef703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views39 pages

Understanding Pell's Equations

This paper investigates Pell Equations, which are equations of the form x² - dy² = k, focusing on integer solutions for various values of k. The author provides proofs for the solvability of the equation when k = 1 and explores the classification of Pell Equations as solvable or unsolvable for other values of k, particularly k = 4. Additionally, the paper discusses patterns in solutions and the use of computers to find these solutions.

Uploaded by

vicofef703
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Solving Pell Equations

Matthew Wright

May 2, 2006

Department of Mathematical Sciences


Messiah College
Grantham, PA

Dr. Lamarr Widmer, advisor


This paper is submitted in partial fulfillment of the
requirements for departmental honors in mathematics.

Abstract
This paper is an investigation of Pell Equations–equations of the form x2 − dy 2 = k
where d is a nonsquare, positive integer, k is an integer, and we are looking for integer
solutions in x and y. We will provide motivation, both algebraic and geometric, for
this definition of Pell Equations. Next, we will examine the k = 1 case, proving not
only that it is solvable, but also that infinitely many solutions can be obtained easily
from the fundamental solution. We will classify some Pell Equations as solvable or
unsolvable when k 6= 1, examining in detail the k = 4 case. After explaining several
patterns that appear when k = 4 and d ≡ 5 (mod 8), we will prove the existence of a
fundamental solution for these cases. Finally, we will briefly examine how a computer
may be used to find solutions, especially the fundamental solution, for Pell Equations.

1
Contents
1 Introduction 3
1.1 Why must d be positive? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 Why must d be nonsquare? . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.3 Pell Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

2 The Equation x2 − dy 2 = 1 is Solvable 6

3 The Fundamental Solution 10


3.1 Sequence of Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.2 Ordering the Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.3 Smallest is Fundamental . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
3.4 Define a and b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.5 Obtain a Contradiction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

4 What About k 6= 1? 15
4.1 Identifying Unsolvable Pell Equations . . . . . . . . . . . . . . . . . . . . . . 15
4.2 If k is a Square . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

5 Investigation of x2 − dy 2 = 4 16
5.1 Cases when d ≡ 5 (mod 8) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
5.2 Odd-Even Solution Patterns . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
5.3 More Patterns in Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

6 The Fundamental Solution Revisited 21


6.1 Preliminary Work . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.2 An Arbitrary Solution (u, v) . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
6.3 Define a and b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6.4 Obtain a Contradiction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

7 Programming 25

8 Table of Solutions 28

References 29

A Listing of PellSolver 30

2
1 Introduction
In number theory, Pell Equations fall in the category of Diophantine equations. Named
after the Greek mathematician Diophantus, Diophantine equations are equations for which
integer solutions are desired. Specifically, Pell Equations have the form x2 − dy 2 = k, where
d and k are fixed and we are looking for integers x and y that satisfy the equation. Some
mathematicians specify that k = 1, but we will allow k to be any nonzero integer. We further
qualify the integer d to be positive and nonsquare. This qualification is helpful because it
leaves open the possibility of infinitely many solutions in positive integers x and y.

1.1 Why must d be positive?


If d were negative, such as d = −p, then the equation of interest becomes
√ x2 + py 2 = k,
q has the form of an ellipse. This ellipse has x-intercepts at ± k and y-intercepts at
which
± kp . By plotting the ellipse on the Cartesian plane (see Figure 1), we can easily see that
q
the integers on the y-axis between 0 and ± kp are the only possible positive integers that
might correspond
q to points on the ellipse with integer coordinates. Therefore, there can be
no more than b kp c solutions in positive integers to the equation x2 + py 2 = k.

k y
√ p

x
√k

Figure 1: Equations of the form x2 + py 2 = 1 have finitely many positive integer solutions
(x, y).

1.2 Why must d be nonsquare?


If d is a square, such as d = q 2 , where q is a positive integer, then the equation becomes
x2 − q 2 y 2 = k, which factors into

(x + qy)(x − qy) = k (1)

3
Since x, y, and q are all positive integers, the left-hand side of (1) must be two integers
whose product is k. For each pair of positive integers m and n, m > n, such that mn = k,
there might exist integers x and y that satisfy
(
x + qy = m
x − qy = n

Therefore, the number of positive integer solutions that satisfy (1) is bounded by half the
number of positive integers which divide k. It could also happen that the equation has no
solutions, as in the case x2 − y 2 = 10.
We can also see that an equation of the form x2 − q 2 y 2 = k has finitely many integer
solutions by the following graphical argument. First, sketch the graph of the hyperbola
x2 − q 2 y 2 = k on the Cartesian plane. Except for a small region near the origin (the region
in Figure 2, for example), the hyperbola is very close to its asymptotes. The asymptotes
are of the form x + qy = 0 and x − qy = 0, so they are lines through the origin with slopes
of ±1/q. The hyperbola might pass through points with integer coefficients near the origin,
but it soon gets too close to its asymptotes to pass through any such points.

(2q, 2)
(q, 1)

–√k √k x

Figure 2: The graph of a hyperbola, x2 − q 2 y 2 = k and its asymptotes, x + qy = 0 and


x − qy = 0.

Figure 3 shows a portion of the asymptote x − qy = 0 on a grid of points with integer


coordinates. Circled points are those closest to the asymptote, but not on the asymptote.
The magnification in Figure 3 helps determine the distance from any such point to the

4
asymptote. Call this distance a. By the Pythagorean Theorem, we have

(aq)2 = a2 + 1
a2 q 2 − a2 = 1
a2 (q 2 − 1) = 1
1
a2 = 2
q −1
r
1
a= 2
q −1
Therefore, q
there are no points with integer coefficients whose distance d from an asymptote
is 0 < d < q21−1

x – qy = 0

aq
a
1

Figure 3: The asymptote x − qy = 0 on a grid of points with integer coordinates.

Let f (x) be the branch of the hyperbola in the first quadrant:


s
x2 − k √
f (x) = , x ≥ k.
q2
x x
We know that limx→∞ f (x) = q
and f (x) 6= q
for any x. So for each  > 0 there exists
x0 > 0 such that
x
x > x0 implies 0 < f (x) − <
q
q
1
Let  = 2
q −1
. Then there exists x0 > 0 such that for all x > x0 , the graph of f (x)
q
is closer than q21−1 from its asymptote, but never touches its asymptote. Therefore, f (x)
does not pass through any integer coordinates for x > x0 . √
We see that f (x) may only pass through integer coordinates when k < x < x0 , so f (x)
cannot pass through infinitely many points with integer coordinates. Therefore, x2 −q 2 y 2 = k
has finitely many integer solutions.

1.3 Pell Equation


Having motivated the choice of d positive and nonsquare, we arrive at the following definition:

5
Definition 1. A Pell Equation is an equation of the form x2 − dy 2 = k, where d is a
positive nonsquare integer and k is a nonzero integer, for which we attempt to find integer
solutions x and y.
Pell Equations have been of interest to mathematicians for centuries. Archimedes’ famous
cattle problem leads to a Pell Equation. The Indian mathematican Brahmagupta in the sixth
century had methods to find solutions to x2 − dy 2 = 1, and he could generate infinitely many
solutions from an initial solution. In Europe, Pierre de Fermat studied the equation and
inspired some of his contemporaries to do the same. John Pell was an English mathematician
who appears to have had a reputation as an algebraist in the middle of the 17th century, but
he did hardly any work with the equation that bears his name [2]. Leonhard Euler credited
the equation to Pell in a letter to Goldbach in 1730, and the name stuck [1].

2 The Equation x2 − dy 2 = 1 is Solvable


Pell Equations where k = 1 are of special interest to mathematicians because they have
infinitely many solutions. In fact, some authors define Pell Equations to be of the form
x2 − dy 2 = 1, not considering equations where k 6= 1 as our broader definition permits. In
this section, we will prove that the equation x2 − dy 2 = 1 has a solution in positive integers
x and y. We will begin with the following lemma:
Lemma 1. For a given integer N , there are positive integers u and v for which
√ 1 1
u−v d < ≤ (2)
N v
and √
u2 − v 2 ≤ 2 d + 1 (3)
To make the proof easier, we introduce some notation. For any number α, we will denote
by hαi the fractional part of α. That is, hαi = α − bαc, where bαc is the greatest integer
that does not exceed α.
Proof. Consider the N + 1 numbers, all of which are between 0 and 1:
√ √ √ √
h di, h2 di, . . . , hN di, h(N + 1) di (4)
Also consider the N intervals {t : Ni < t < i+1
N
}, where 0 ≤ i ≤ N −1. Each of these intervals
has width 1/N . Note that together, this set of intervals spans the numbers from 0 to 1, except
for the the numbers i/N , which are the endpoints of the intervals. These boundaries, i/N ,
never equal any of the numbers in (4) since all of the numbers are irrational.
By the pigeonhole principle, if we place the N + 1 numbers in the N intervals,√at least
one
√ interval must contain at least two of the numbers. Call two of these numbers hq di and
i i+1

hs di, with q > s. Call the interval that contains these numbers N , N . Therefore, we
have
i √ i+1 i √ i+1
< hq di < and < hs di <
N N N N
6
√ √
√Define √p and r√ to be the integer
√ parts
√ of q d
√ and s d, respectively. Therefore, p −
bq dc = q d − hq √ di and r =√bs dc =√s d − hs di.
Since q > s and d > 1, bq dc > bs dc and p > r.
Since q and s are each between 1 and N + 1, their difference cannot exceed N . Therefore,
q − s ≤ N. √ √
Since hq di and hs di both fall in the same interval of width 1/N , their difference is
less than 1/N . Therefore,
√ √ 1
hq di − hs di <
N
 √   √  1
q d−p − s d−r <
N
√ 1
(q − s) d − (p − r) <
N
√ 1
(p − r) − (q − s) d <
N
Let u = p − r and v = q − s. Then q − s = v ≤ N , so v1 ≥ N1 . Therefore, we have found two

integers u and v that satsify u − v d < N1 ≤ v1 , or more importantly

√ 1
u−v d ≤ (5)
v
which proves the first part of Lemma 1.
To prove the second part of Lemma 1, we begin with the following identity:
√ √ √
u + v d = (u − v d) + 2v d (6)

Multiplying equations (5) and (6), we have


√ √ 1 √ √
(u + v d) u − v d ≤ [(u − v d) + 2v d]
v √
2 2 u−v d √
u − dv ≤ +2 d (7)
v
Beginning with inequality (5), we arrive at the following:
√ 1
u−v d ≤
√ v
u−v d √ √ 1
≤u−v d≤ u−v d ≤ ≤1
v v
so √
u−v d
≤1 (8)
v

7
Combining inequalities (7) and (8), we have

2 2 u−v d √ √
u − dv ≤ +2 d≤1+2 d
v
Therefore, we conclude that √
u2 − dv 2 ≤ 2 d + 1
and this proves the second part of Lemma 1.

In proving Lemma 1, we began by choosing the first N + 1 consecutive multiples of d
and examining their fractional parts:
√ √ √ √
h di, h2 di, . . . , hN di, h(N + 1) di (9)

Now, there are infinitely many non-overlapping sets of N +1 consecutive multiples of d that
we could have chosen, and each set works equally well in proving the lemma. For example,
we could have chosen either of the following:
√ √ √
h(N + 2) di, h(N + 3) di, . . . , h(2N + 2) di
√ √ √
h(2N + 3) di, h(2N + 4) di, . . . , h(3N + 3) di

In any such set, we can find two numbers whose fractional parts lie in the same interval.
Therefore, each set produces the numbers u and v that satisfy the equations of Lemma 1.
Since we can choose non-overlapping sets, u and v will be unique to each set. Therefore, we
can find infinitely many pairs (u, v) that satisfy
√ 1 1 √
u−v d < ≤ and u2 − v 2 ≤ 2 d + 1 (10)
N v
Note that |u2 − dv 2 | is an integer. Therefore, there
√ are infinitely many solutions to
2 2
u − dv = j, where j is an integer bounded by |j| ≤ 2 d + 1. Since we have finitely many
possibilities for j and infinitely many solutions (u, v), there must be at least one j with
infinitely many solutions (u, v). Let k be this particular value of j. Therefore, there are
infinitely many solutions (u, v) to u2 − dv 2 = k.
The second lemma we will use in our proof is the following:

Lemma 2. If (x1 , y1 ) and (x2 , y2 ) are integer solutions of x2 − dy 2 = k such that

x1 ≡ x2 (mod k) and y1 ≡ y2 (mod k)

Then the equation x2 − dy 2 = 1 has a solution in positive integers.

Proof. We have the following two equations:

x21 − dy12 = k and x22 − dy12 = k (11)

8
Multiplying the equations together, we have

(x21 − dy12 )(x22 − dy22 ) = k 2


x21 x22 − dx21 y22 − dx22 y12 + d2 y12 y22 = k 2
x21 x22 − 2dx1 x2 y1 y2 + d2 y12 y22 − d x21 y22 − 2dx1 x2 y1 y2 + x22 y12 = k 2
 

(x1 x2 − dy1 y2 )2 − d (x1 y2 − x2 y1 )2 = k 2


 2  2
x1 x2 − dy1 y2 x1 y2 − x2 y1
−d =1 (12)
k k

Equation (12) has the desired form, so the lemma is proved if we can show that the quantities
in parentheses are integers.
Since x1 ≡ x2 (mod k) and y1 ≡ y2 (mod k), then

x1 x2 − dy1 y2 ≡ x1 x1 − dy1 y1 = x21 − dy12

But x21 − y12 = k and k ≡ 0 (mod k), so x21 − dy12 ≡ 0 (mod k). Therefore,

x1 x2 − dy1 y2 ≡ x21 − dy12 ≡ 0 (mod k) (13)

Also, we see that x1 y2 ≡ x2 y1 (mod k), which implies that

x1 y2 − x2 y1 ≡ 0 (mod k) (14)

Combining the results of equations (13) and (14), we see that the quantities in parentheses
in equation (12) are integers, so
 
x1 x2 − dy1 y2 x1 y2 − x2 y1
,
k k

is an integer solution of x2 − dy 2 = 1 and we have proved Lemma 2.


We are now ready to prove the main theorem of this section:

Theorem 1. The equation x2 − dy 2 = 1 has a solution in positive integers x and y for all
positive, nonsquare integers d.

Proof. We have found a value of k for which there exist infinitely many solutions (u, v) of
u2 − dv 2 = k. There are only k 2 unique ordered pairs modulo k. Since we have more than
k 2 + 1 solutions (each of which is an ordered pair), at least two of them must be equivalent
modulo k. Call these two pairs (xi , yi ) and (xj , yj ), with xi ≡ xj and yi ≡ yj (mod k). By
Lemma 2, the equation x2 − dy 2 = 1 has a solution in positive integers.

9
3 The Fundamental Solution
In the previous section we proved that there exists a positive integer solution (x, y) to the
equation x2 − dy 2 = 1 when d is positive and nonsquare, In this section we will see that
there is a solution, known as the fundamental solution, from which all other positive integer
solutions may be obtained. Specifically, we will prove that the fundamental solution is
smallest positive integer solution to x2 − dy 2 = 1. We will also see how all other positive
integer solutions can be obtained from the fundamental solution.

3.1 Sequence of Solutions


From any solution of x2 − dy 2 = 1, we can obtain infinitely many solutions. Suppose (x1 , y1 )
is a solution of x2 − dy 2 = 1. We can obtain another solution by the following process:

x21 − dy12 = 1
√ √
(x1 + y1 d)(x1 − y1 d) = 1
√ √
(x1 + y1 d)2 (x1 − y1 d)2 = 1
 √  √ 
x21 + 2x1 y1 d + dy12 x21 − 2x1 y1 d + dy12 = 1
 √  √ 
(x21 + dy12 ) + (2x1 y1 ) d (x21 + dy12 ) − (2x1 y1 ) d = 1
2
x21 + dy12 − d (2x1 y1 )2 = 1

We have again obtained the form of a Pell Equation, and we can see that (x21 + dy12 , 2x1 y1 )
is a solution. Applying the process again, we can obtain yet another solution, and so on,
obtaining as many solutions as we desire. √
Alternately, suppose we consider integer powers of (x1 + y1 d):
 √ n √
x1 + y1 d = xn1 + C1 dxn−1 1 y1 + C2 dxn−21 y12 + C3 d3/2 xn−3
1 y13 + · · · + dn/2 y1n
 √
= xn1 + C2 dxn−2 y12 + · · · + d C1 xn−1 y1 + C3 xn−3 y13 + · · ·

1 1 1

= xn + yn d

where Ci are the binomial coefficients: Ci = ni = (n−i)!i!n!



. Since each integer power can be
written in a form similar to the original, we define xn and yn as follows:
√  √ n
xn + yn d = x1 + y1 d (15)

We will now prove the following theorem:


 √   √ n
Theorem 2. If x1 > 1, y1 ≥ 1, and xn + yn d = x1 + y1 d , then xn+1 > xn and
yn+1 > yn for positive n.

10
Proof by Induction. Initial Case: We find that x2 = x21 + dy12 and y2 = 2x1 y1 . Since x1 > 1,
y1 ≥ 1, and d is a positive integer, it is clear that x2 > x1 and y2 > y1 .
Induction: Assume we have a solution (xn , yn ) with xn and yn positive integers greater
than 1. We have:
√  √ n+1
xn+1 + yn+1 d = x1 + y1 d
 √  √ n
= x1 + y1 d x1 + y1 d
 √  √ 
= x1 + y1 d xn + yn d

= (x1 xn + dy1 yn ) + (x1 yn + xn y1 ) d

Therefore, xn+1 = x1 xn + dy1 yn and yn+1 = x1 yn + xn y1 . Note that x1 xn > xn and dy1 yn > 0.
Thus, xn+1 = x1 xn + dy1 yn > xn . Also, x1 yn > yn and xn y1 > 0, so yn+1 = x1 yn + xn y1 > yn .
Therefore, we have xn+1 > xn and yn+1 > yn .
We have established that the sequences {xn } and {yn } are strictly increasing when defined

according to equation (15). This suggests that if we want the integer powers of (x1 + y1 d)
to generate all positive integer solutions of x2 −dy 2 = 1, then x1 and y1 should be as small as
possible. First, however, we must answer the question: Does the solution with the smallest
value of x also contain the smallest value of y?

3.2 Ordering the Solutions


To answer the question posed above, we must examine the ordering of the solutions. It
turns out that the solution with the smallest value of x does contain the smallest value of y.
Specifically, we will prove the following theorem:

Theorem 3. If p, q, r, and s are positive integers for which p > r and

p2 − dq 2 = r2 − ds2 = k (16)
√ √
then q > s and p + q d > r + s d.

Proof. Since p > r and r ≥ 1, it follows that p2 > r2 . We can rewrite equation (16) as
p2 − r2 = d(q 2 − s2 ). Since p2 − r2 > 0 and d > 0, it follows that q 2 − s2 > 0, and q 2 > s2 .
Therefore, q > s. √ √ √
Next,
√ we have
√ p > r and q > s. Since d is positive, q d > s d. It follows that
p + q d > r + s d.
Therefore, we now define x1 and y1 so that both are positive integers and x1 is the smallest
positive integer x satisfying the equation x2 − d2 = 1. We know from Theorem 3 that y1
is the smallest positive integer y satisfying the equation, so (x1 , y1 ) is the smallest positive
integer solution to x2 − dy 2 = 1.

11
3.3 Smallest is Fundamental
In this section, we will show that any arbitrary positive integer solution of x2 − dy 2 = 1 can
be obtained from the smallest positive integer solution. Specifically, suppose that u > 0,
2 2
v > 0,√ and u − dv√ =m1. We will show that there exists a positive integer m such that
u + v d = (x1 + y1 d) .
Since x1 is the smallest positive integer √satisfying x2√− dy 2 = 1, it must be true that
u ≥ x1 . If√
u > x1 , then √by Theorem 3, u + v d > x1 + y1 d. If u = x1 , then clearly v = y1 ,
and u + v d = x1 + y1 d. Therefore, we have
√ √
u + v d ≥ x1 + y1 d (17)
Next, we will define the positive integer m by the condition
 √ m  √   √ m+1
x1 + y1 d ≤ u + v d < x1 + y1 d (18)

That is, the mth solution generated from (x1 , y1 ) is the largest solution not exceeding (u.v).
Another way of stating equation (18) is
√ √ √
xm + ym d ≤ u + v d < xm+1 + ym+1 d

√We−mwill now assemble an important inequality in three parts. First, consider (x1 +
y1 d) :
√ √
√ −m 1 (x1 − y1 d)m (x1 − y1 d)m
(x1 + y1 d) = √ · √ = (19)
(x1 + y1 d)m (x1 − y1 d)m (x21 − dy12 )m
Since x21 − dy12 = 1, the denominator in (19) is 1, and we have
√ √
(x1 + y1 d)−m = (x1 − y1 d)m
Therefore, √ √ √ √
(u + v d)(x1 + y1 d)−m = (u + v d)(x1 − y1 d)m (20)
Second, the following begins with the left inequality of (18):
√ √
(x1 + y1 d)m ≤ (u + v d)
√ √ √ √
(x1 + y1 d)m (x1 + y1 d)−m ≤ (u + v d)(x1 + y1 d)−m
√ √
1 ≤ (u + v d)(x1 + y1 d)−m (21)
Third, the following begins with the right inequality of (18):
√ √
(u + v d) < (x1 + y1 d)m+1
√ √ √ √
(u + v d)(x1 − y1 d)m < (x1 + y1 d)m+1 (x1 − y1 d)m
√ √ √ √
(u + v d)(x1 − y1 d)m < (x1 + y1 d)m+1 (x1 + y1 d)−m
√ √ √
(u + v d)(x1 − y1 d)m < (x1 + y1 d) (22)

12
Combining equation (20) with inequalities (21) and (22), we have the following inequality:
√ √ √ √ √
1 ≤ (u + v d)(x1 + y1 d)−m = (u + v d)(x1 − y1 d)m < (x1 + y1 d) (23)
Inequality (23) is key to the rest of the proof. Specifically, we will show
√ that the√leftmost
inequality is in fact an equality. That is, we will show that 1 = (u + v d)(x1 + y1 d)−m .

3.4 Define a and b


Suppose that a and b are integers determined by
√ √ √
a + b d = (u + v d)(x1 − y1 d)m (24)
We will make three observations. First, we will show that a2 − db2 = 1.
√ √ √
a + b d = (u + v d)(x1 − y1 d)m
√ √ √
a + b d = (u + v d)(xm − ym d)
√ √
a + b d = (uxm − dvym ) + d(vxm − uym ) (25)
By equation (25), we can see that a2 − db2 = 1 as follows:
a2 − db2 = (uxm − dvym )2 − d(vxm − uym )2
h √ ih √ i
= (uxm − dvym ) + d(vxm − uym ) (uxm − dvym ) − d(vxm − uym )
√ √ √ √
= (u + v d)(xm − ym d)(xm + ym d)(u − v d)
= (u2 − dv 2 )(x2m − dym
2
)
=1
√ √
Second, we will show that a − b d = (a + b d)−1 > 0. To begin,
a2 − db2 = 1
√ √
(a + b d)(a − b d) = 1 (26)
√ √ −1
(a − b d) = (a + b d)
√ √ m
√ we know that 1 ≤ (u + v d)(x1 −√y1 d) . Combining this with
From inequality (23), √ the
definition of a + b d from (24), we see that √ 1 ≤ a + b d which implies that a + b d is
positive. The multiplicative inverse of a + b d must also be positive, so from equation (26),
we see √ √
(a − b d) = (a + b d)−1 > 0. (27)
√ √
Third,
√ we will √ show that a − b d ≤ 1 ≤ a + b d. We√know from equation (26) that
(a + b √ d)(a − b d) = 1, and
√ we just stated that 1 ≤ (a + b d). The multiplicative inverse
of a + b d, which is a − b d, must be less than one. Threrefore,
√ √
a − b d ≤ 1 ≤ a + b d. (28)

13
This completes our three observations about a and b.
In order for the inequality (28) to hold, b must be greater than or equal to zero. Here’s
why:
√ √
a−b d≤a+b d
√ √
−b d ≤ b d

0 ≤ 2b d
0≤b
√ √
From √inequality (27), we have a − b d > 0 which implies a > b d. Since b ≥ 0, it follows
that b d ≥ 0, and a > 0. Therefore, we have the following conditions on a and b:

a>0 and b ≥ 0. (29)

3.5 Obtain a Contradiction


By equation (24) and inequality (22), we have:
√ √ √ √
a + b d = (u + v d)(x1 − y1 d)m < (x1 + y1 d)
√ √
a + b d < x1 + y 1 d (30)
√ √
Since a + b d ≯ x1 + y1 d, then by the contrapositive of Theorem 3, a and b are not both
positive integers. (Note that everything else in the hypothesis
√ of 3 must √ be true: we defined
x1 and y1 to be positive integers, and we know that a2 +b2 d = x21 +y12 d = 1. Additionally,
x-value that satisfies x2 − dy 2 = 1 and a 6= x1
if a is positive, a > x1 since x1 is√the smallest√
since a = x1 would imply a + b d = x1 + y1 d, which is false.) We know that a > 0 and
b ≥ 0 from equation (29). The only way to satisfy these conditions and make a and b not
both positive is to let b = 0. In this case, a = 1 since a2 − db2 = 1. Therefore, by the
defintion of a and b in equation (24), we have
√ √ √
a + b d = (u + v d)(x1 − y1 d)m
√ √
1 = (u + v d)(x1 − y1 d)m
√ √
1 = (u + v d)(x1 + y1 d)−m . (31)
√ √
By equation (31), (u + v d) = (x1 + y1 d)m . We have now proved the following theorem:
2 2
Theorem 4. If (x, y) = (u, v) is a positive
√ √ m of x − dy = 1, then there exists
integer solution
a positive integer m such that u + v d = (x1 + y1 d) , where (x1 , y1 ) is the fundamental
solution of x2 − dy 2 = 1.

Proof. The proof is contained in section 3.3.

14
4 What About k 6= 1?
We have proved the existence of a fundamental solution for equations of the form x2 −dy 2 = 1,
but what happens for other values of k? Sometimes solutions exist for particular values of
d and k, and other times no solution can be found. With a definition and a theorem we can
quickly identify many Pell Equations that have no solution at all.

4.1 Identifying Unsolvable Pell Equations


Definition 2. An integer a is a quadratic residue modulo m if there exists an integer n,
0 < n < m, such that n2 ≡ a (mod m). If no such integer n exists, then a is a quadratic
nonresidue modulo m.
Theorem 5. If k is a quadratic nonresidue modulo d, then the Pell Equation x2 − dy 2 = k
has no integer solution.
Proof. First, rewrite the Pell Equation as:
x2 − k
y2 = . (32)
d
The right-hand side of equation (32) must be an integer if it is to be a perfect square. This
implies that x2 − k must be divisible by d, or x2 − k ≡ 0 (mod d). In other words, x can
only be a solution if x2 ≡ k (mod d). But this can only happen if k is a quadratic residue
modulo d. If k is a quadratic nonresidue modulo d, then no integer x exists such that x2 ≡ k
(mod d), and there cannot possibly be an integer y that satisfies equation (32). Therefore,
the Pell Equation x2 − dy 2 = k has no integer solution if k is a quadratic nonresidue modulo
d.
Theorem 5 can identify Pell Equations for which no integer solution exists. The inverse
of Theorem 5 is not true. For example, 7 ≡ 1 is a quadratic residue modulo 3, but the
equation x2 − 3y 2 = 7 does not have an integer solution.1
Additionally, the proof of Theorem 5 can help us search for solutions to a Pell Equation.
Suppose we wish to look for a solution to x2 − dy 2 = k by choosing an x-value and checking
to see if the corresponding y-value is an integer. Which x-values should we test? Provided
that k is a quadratic residue modulo d, the proof above suggests that we should only choose
x-values whose square is congruent to k modulo d. Our task is especially easy if d is a prime,
say d = p. Since Zp , the set of integers modulo p, is a field, there are exactly two integers in
Zp whose squares are congruent to k modulo d. Specifically, if one of these two integers is m,
the other is p − m. Therefore, the set of x-values that make the right-hand side of equation
(32) an integer are:
{m, p − m, p + m, 2p − m, 2p + m, 3p − m, . . .}
1
The fact that x2 − 3y 2 = 7 has no solution can be verified by considering the integers modulo 4. The
only quadratic residues modulo 4 are 0 and 1, so x2 and y 2 must each be congruent to either 0 or 1 modulo
4. In this case, x2 − 3y 2 will never be congruent to 3 modulo 4. Therefore, x2 − 3y 2 6= 7.

15
Our task would then be to test each of these values until we (hopefully) find one that makes
(x2 − k)/d a perfect square.

4.2 If k is a Square
While the previous section identified many Pell Equations without solutions, we can also
identify many Pell Equations with k 6= 1 that have solutions. Consider the following theorem:

Theorem 6. If k is a perfect square, then the Pell Equation x2 − dy 2 = k is solvable in


integers for all positive, nonsquare integers d.

Proof. Since k is a perfect square, let k = m2 . Theorem 1 says that the equation x2 −dy 2 = 1
is solvable in integers for all positive, nonsquare integers d. Let (u, v) be a solution, and we
have:
u2 − dv 2 = 1 (33)
Multiply both sides of equation (33) by m2 to obtain:

m2 (u2 − dv 2 ) = 1 · m2
m2 u2 − dm2 v 2 = m2
(mu)2 − d(mv)2 = k

Therefore, (mu, mv) is a solution to x2 − dy 2 = k where x = m2 . We see that beginning with


any solution of x2 − dy 2 = 1 we can produce a solution of x2 − dy 2 = k simply by multiplying
our given solution by m.
In fact, Pell Equations with k square have infinitely many solutions. Pell Equations
where k = 1 have infinitely many solutions, each of which corresponds to a solution in the
case where k is a square.

5 Investigation of x2 − dy 2 = 4
Having proved that equations of the form x2 − dy 2 = 4 have integer solutions, we will now
investigate patterns that occur in such solutions. Since 4 = 22 , we know by Theorem 6 that
if (u, v) is a solution of x2 − dy 2 = 1, then (2u, 2v) is a solution of x2 − dy 2 = 4. This
method of finding solutions obtains only even solutions. Might odd solutions ever exist to
Pell Equations with k = 4? The following theorem limits the values of d for which odd
solutions might appear:

Theorem 7. If x2 − dy 2 = 4 can be solved for odd integers x and y, then d ≡ 5 (mod 8).

16
Proof. Let x and y be odd integers such that x2 − dy 2 = 4. Note that the square of any odd
integer is congruent to 1 (mod 8), so x2 ≡ y 2 ≡ 1 (mod 8).

x2 − dy 2 = 4
1−d≡4 (mod 8)
−d ≡ 3 (mod 8)
d≡5 (mod 8)

Therefore, d ≡ 5 (mod 8).


Theorem 7 says that if odd solutions exist, then d ≡ 5 (mod 8), but it does not provide
any examples of odd solutions. We will investigate equations x2 − dy 2 = 4 with d ≡ 5
(mod 8) to see if odd solutions actually exist.

5.1 Cases when d ≡ 5 (mod 8)


It is not difficult to find odd integers x and y that satisfy an equation of the form x2 −dy 2 = 4
with d ≡ 5 (mod 8). Considering the case d = 5, we find that the first solution is x = 3,
y = 1. The eleven smallest positive integer solutions appear in Table 1.

x y
3 1
7 3
18 8
47 21
123 55
322 144
843 377
2207 987
5778 2584
15127 6765

Table 1: Solutions to x2 − 5y 2 = 4

Observe in Table 1 that every third solution is even, while the other solutions are odd.
Furthermore, the even solutions are those guaranteed by Theorem 6; that is, they are twice
the integer solutions of x2 − 5y 2 = 1. For example, (18, 8) is a solution of x2 − 5y 2 = 4, and
(9, 4) is a solution of x2 − 5y 2 = 1. Does this pattern of odd and even solutions continue,
and does it exist in the solutions for other Pell Equations with d ≡ 5 (mod 8) and k = 4?
Examining the solutions to equations with d = 13 and d = 21 (see Tables 2 and 3), we again
see the pattern of two odd solutions followed by one even solution. Again, each even solution
corresponds to a solution of the equation with k = 1. Why might this be?

17
x y
11 3
119 33
1298 360
14159 3927
154451 42837
1684802 467280
18378371 5097243
200477279 55602393
2186871698 606529080

Table 2: Solutions to x2 − 13y 2 = 4

x y
5 1
23 5
110 24
527 115
2525 551
12098 2640
57965 12649
277727 60605
1330670 290376
6375623 1391275

Table 3: Solutions to x2 − 21y 2 = 4

5.2 Odd-Even Solution Patterns


In this section, we will use the convention that (x1 , y1 ) is the smallest positive integer solution
to x2 − dy 2 = 4. Suppose that (xn , yn ) satisfies x2n − dyn2 = 4n . Then,

(x21 − dy12 )(x2n − dyn2 ) = 4 · 4n


√ √ √ √
(x1 + y1 d)(xn + yn d)(x1 − y1 d)(xn − yn d) = 4n+1
 √  √ 
(x1 xn + dy1 yn ) + (x1 yn + xn y1 ) d (x1 xn + dy1 yn ) − (x1 yn + xn y1 ) d = 4n+1
(x1 xn + dy1 yn )2 − d(x1 yn + xn y1 )2 = 4n+1

Therefore, we see that solutions (xn , yn ) of x2n − dyn2 = 4n can be found recursively by

xn+1 = x1 xn + dy1 yn and yn+1 = x1 yn + xn y1 . (34)

18
Note that since (xn , yn ) satisfy x2n − dyn2 = 4n , solutions to x2 − dy 2 = 4 are given by
xn
, yn .

2n−1 2n−1
The following theorem, then, explains the pattern of odd and even solutions that we have
seen in some Pell Equations with d ≡ 5 (mod 8) and k = 4:

Theorem 8. Suppose that d ≡ 5 (mod 8) and the smallest positive integer solution of x2 −
dy 2 = 4 is (x1 , y1 ), with x1 and y1 both odd. With xn and yn as defined in equation (34), the
first solution to be divisible by 2n occurs at n = 3 and provides a solution of x2 − dy 2 = 1.

Proof. First, we have x21 − dy12 = 4 with x1 and y1 both odd. By equation (34) and the fact
that the square of any odd integer is congruent to 1 (mod 8), x2 and y2 are congruent to:

x2 ≡ x21 + 5y12 ≡ 6
y2 ≡ 2x1 y1 ≡ 2 or 6

In either case, x2 and


 y2 contain exactly one factor of two, so we can divide by two and
x2 2 y2 2

obtain 2 − d 2 = 4, and we have the second odd solution to x − dy 2 = 4.
2

Next, x3 and y3 are congruent to:

x3 ≡ x1 x2 + 5y1 y2 ≡ x1 (x21 + 5y12 ) + 5y1 (2x1 y1 ) ≡ x31 + 5x1 y12 + 10x1 y12
≡ x1 + 5x1 + 2x1 ≡ 8x1 ≡ 0
and
y3 ≡ y1 x2 + x1 y2 ≡ y1 (x21 + 5y12 ) + x1 (2x1 y1 ) ≡ x21 y1 + 5y13 + 2x21 y1
≡ y1 + 5y1 + 2y1 ≡ 8y1 ≡ 0

We see that x3 and y3 both contain a factor of 23 . Thus, we can divide them by 4 to obtain
a solution to x2 − dy 2 = 4, or by 8 to obtain a solution to x2 − dy 2 = 1.
Furthermore, we can observe that x3 and y3 do not both contain factors of 16. If they did,
then x3 /8 and y3 /8 would both be even, and we would not have a solution of x2 − dy 2 = 1.
2 2
Since x83 − d y83 = 1, one of x3 and y3 has a factor of 8 and not 16.
The next theorem shows that if the initial solution of x2 − dy 2 = 4 is odd, then every
third solution corresponds to a solution of x2 − dy 2 = 1.

Theorem 9. If xn and yn are solutions of x2n − dyn2 = 4n , with d ≡ 5 (mod 8), and x1 and
y1 (the smallest such solution) are both odd, and xk and yk satisfy
 x 2  y 2
k k
k
−d = 1,
2 2k
then xk+3 and yk+3 are the next solution to provide a solution to x2 − dy 2 = 1.

19
Proof. As in the previous proof, we use the recursion defined in equation (34) to obtain
successive solutions to x2n − dyn2 = 4n . We obtain the following congruences:

xk+1 ≡ x1 xk + 5y1 yk
yk+1 ≡ x1 yk + y1 xk

xk+2 ≡ 6xk + 2x1 y1 yk


yk+2 ≡ 2x1 xk y1 + 6yk

xk+3 ≡ 8x1 xk + 8y1 yk ≡ 0


yk+3 ≡ 8xk y1 + 8x1 yk ≡ 0
2 2
Since x2kk − d 2ykk = 1, we know that xk and yk each contain k multiples of 2, and they do
not both contain k + 1 multiples of 2. With the first iteration, x k+1 and yk+1 both contain
exactly k multiples of 2, providing the odd solution xk+1 2k
, yk+1
2k
to x2 − dy 2 = 4. At the
second iteration, we gain a factor of 2, so xk+2 and yk+2 both contain exactly k + 1 factors of
2, providing a solution to x2 − dy 2 = 4. At the third iteration, the coefficients of 8 indicate
that both xk+3 and yk+3 have three more factors of 2 than xk and yk . We see that x2k+3 k+3
and
yk+3 2 2
2k+3
are both integers, and they satisfy x − dy = 1. Therefore, every third solution of
x − dy 2 = 4 is even, and can be divided by two to obtain a solution of x2 − dy 2 = 1.
2

Corollary 1. If x1 and y1 are both odd, then x3i and y3i always provide a solution to
x2 − dy 2 = 1 for any positive integer i.
Proof. Theorem 8 says that if x1 and y1 are both odd, then the first even solution of x2 −dy 2 =
4 is (x3 , y3 ). Theorem 9 says that if (xk , yk ) is an even solution of x2 − dy 2 = 4, then the next
even solution is (xk+3 , yk+3 ). Therefore, all of the even solutions occur with subscripts that
are multiples of 3. In other words, (x3i , y3i ) is always an even solution, so it can be divided
x3i y3i

by 2 to obtain 2 , 2 ), which is a solution of x2 − dy 2 = 1.

5.3 More Patterns in Solutions


If we examine solutions to Pell Equations x2 − dy 2 = 4 for many values of d, d ≡ 5 (mod 8),
we encounter many patterns. Table 4 lists the smallest positive integer solution for the first
30 Pell Equations of this form (5 ≤ d ≤ 237). First, we observe that for some values of d,
the smallest solution is even. These cases are indicated in bold in Table 4. In these cases,
Theorems 8 and 9 do not apply, since they require that x1 and y1 be odd. The author does
not know why the smallest positive integer solution of x2 − dy 2 = 4 is even for some values
of d ≡ 5 (mod 8), but such values of d appear to be scattered haphazardly through Table 4.
Additionally, in these cases, the only solutions to x2 − dy 2 = 4 are those that are twice the
solutions to x2 − dy 2 = 1.
Notice that each odd integer greater than 1 appears as an x-value in the solution for some
d in the table. Also notice that these odd integers also appear as y-values in the solution for

20
d x y d x y d x y
5 3 1 85 83 9 165 13 1
13 11 3 93 29 3 173 171 13
21 5 1 101 402 40 181 1703027 126585
29 27 5 109 68123 6525 189 110 8
37 146 24 117 11 1 197 786 56
45 7 1 125 123 11 205 43 3
53 51 7 133 173 15 213 73 5
61 1523 195 141 190 16 221 15 1
69 25 3 149 3723 305 229 227 15
77 9 1 157 45371 3621 237 77 5

Table 4: Solutions to x2 − dy 2 = 4 for d ≡ 5 (mod 8)

d following the row in which they appeared as an x-value. We can explain these occurrances
by the following two theorems:
Theorem 10. Let m be odd and d = m2 − 4. Then d ≡ 5 (mod 8) and (x, y) = (m, 1) is
the solution to x2 − dy 2 = 4.
Proof. Since m is odd, m2 ≡ 1 (mod 8). Then d = m2 − 4 ≡ −3 ≡ 5 (mod 8). Let
(x, y) = (m, 1), and it follows that:
x2 − dy 2 = m2 − (m2 − 4)(1)2 = m2 − m2 + 4 = 4
Therefore, (x, y) = (m, 1) is the solution of x2 − dy 2 = 4 with d = m2 − 4.
The previous proof explains why the list of solutions of x2 − dy 2 = 4, d ≡ 5 (mod 8),
contains solutions (m, 1) for every odd integer m (see Table 4). The next theorem explains
the solutions (m2 + 2, m).
Theorem 11. Let m be odd and d = m2 + 4. Then d ≡ 5 (mod 8) and (x, y) = (m2 + 2, m)
is the solution to x2 − dy 2 = 4.
Proof. Since m is odd, m2 ≡ 1 (mod 8). Then d = m2 + 4 ≡ 5 (mod 8). Let (x, y) =
(m2 + 2, m), and it follows that:
x2 − dy 2 = (m2 + 2)2 − (m2 + 4)m2 = m4 + 4m2 + 4 − m4 − 4m2 = 4
Therefore, (x, y) = (m2 + 2, m) is the solution of x2 − dy 2 = 4 with d = m2 + 4.

6 The Fundamental Solution Revisited


We will now prove the most exciting result of our work with x2 − dy 2 = 4. There exists a
fundamental solution to x2 − dy 2 = 4, d ≡ 5 (mod 8) from which all other solutions can be
obtained. This proof is similar to the proof from section 3 of the existence of a fundamental
solution for equations of the form x2 − dy 2 = 1.

21
6.1 Preliminary Work
From any solution to x2 − dy 2 = 4, we can obtain another solution as follows. Let (x1 , y1 )
be a solution. Then,

x21 − dy12 = 4
√ √
(x1 + y1 d)(x1 − y1 d) = 4
√ √
(x1 + y1 d)2 (x1 − y1 d)2 = 42
 √  √ 
x1 + 2x1 y1 d + dy1 x1 − 2x1 y1 d + dy1 = 42
2 2 2 2

 √  √ 
(x21 + dy12 ) + (2x1 y1 ) d (x21 + dy12 ) − (2x1 y1 ) d = 42
2
x21 + dy12 − d (2x1 y1 )2 = 42
 2 2 2
x1 + dy12

2x1 y1
−d =4
2 2

Therefore, for any solution (x1 , y1 ) of x2 − dy 2 = 4, define xn and yn such that



√ (x1 + y1 d)n
xn + yn d = . (35)
2n−1
√ √ n
It follows that (xn − yn d) = (x1 −y 2
1 d)
n−1 and x2n − dyn2 = 4. Moreover, xn+1 > xn and
yn+1 > yn ). As in section 3, this suggests that if x1 and y1 are to generate all the other
solutions, then they should be as small as possible. Therefore, we will define (x1 , y1 ) to be a
positive integer solution to x2 − dy 2 = 4, d ≡ 5 (mod 8), with x1 as small as possible.
Recall from Theorem 3 that if (p, q) and (r,√ s) are both √positive integer solutions to
2 2
x − dy = 4 and p > r, then q > s and p + q d > r + s d. This helps us order the
solutions of x2 − dy 2 = 4, since the solution with the mth -smallest x-value also contains the
mth -smallest y-value.

6.2 An Arbitrary Solution (u, v)


Suppose that (u, v) is an arbitrary positive integer solution of x2 − dy 2 = 4. Since x1 √ is the
2 2
smallest √ positive integer x satisfying x − dy = 4, then u ≥ x1 . If u > x1 , √then u + v √d>
x1 + y1 d by the above paragraph.
√ If u√= x1 , then clearly v = y1 and u + v d = x1 + y1 d.
Therefore, we have u + v d ≥ x1 + y1 d.
As in Section 3, we define the positive integer m so that the mth solution generated from
(x1 , y1 ) is the largest not exceeding (u, v). That is,
√ √ √
xm + ym d ≤ u + v d < xm+1 + ym+1 d
√ √
(x1 + y1 d)m √ (x1 + y1 d)m+1
≤u+v d< (36)
2m−1 2m

22
Working with inequality (36):
√ √
(x1 + y1 d)m √ (x1 + y1 d)m+1
m−1
≤u+v d<
√ m 2 √ m 2√m √ √
(x1 − y1 d) (x1 + y1 d) √ (x1 − y1 d)m (x1 + y1 d)m+1 (x1 − y1 d)m
≤ (u + v d) <
2m 2m−1 2m√ 2m 2m
4m √ (x1 − y1 d)m √ 4m
≤ (u + v d) < (x 1 + y 1 d) m
22m−1 2m√ 4
√ (x1 − y1 d)m √
2 ≤ (u + v d) < x 1 + y1 d (37)
2m
Our task is now to show that the left-hand inequality of (37) is, in fact, an equality.

6.3 Define a and b


Suppose that a and b are the positive integers determined by

√ √ (x1 − y1 d)m
a + b d = (u + v d) . (38)
2m
In Section 3.3, it was obvious that a and b must be integers, but this time it is not obvious,
since we are dividing by 2m . We can prove that a and b must be integers as follows:

√ √ 1 (x1 − y1 d)m
a + b d = (u + v d) ·
2 2m−1
√ 1 √ √
a + b d = (u + v d)(xm − ym d)
2
√ uxm − dvym vxm − uym √
a+b d= + d
2 2
Since (u, v) and (xm , ym ) both satisfy x2 − dy 2 = 4 and d ≡ 5 (mod 8), u and v have the
same parity, and xm and ym have the same parity. If either (u, v) or (xm , ym ) are even, then
(uxm − dvym , vxm − uym ) is even. Otherwise, both (u, v) and (xm , ym ) are both odd, but
(uxm − dvym , vxm − uym ) is still even. Therefore, a = uxm −dvy
2
m
and b = vxm −uy
2
m
are both
integers.

23
Consider a2 − db2 :
 2  2
2 2 uxm − dvym vxm − uym
a − db = −d
2 2
1  √  √ 
= (uxm − dvym ) + d(vxm − uym ) (uxm − dvym ) − d(vxm − uym )
4
1  √ √  √ √ 
= (u + v d)(xm − ym d) (xm + ym d)(u − v d)
4
1
= (u2 − dv 2 )(x2m − dym
2
)
4
1
= ·4·4
4
=4

Therefore, x2 − db2 = 4. √ √ √ √
Since a2 − db2 = 4, we have√(a − b d)(a + b d) = 4, so a + b d and
√ a − b d have
√ the
same sign. However, 2 ≤ a + b d by inequality (37). Therefore, a + b d and a − b d are
both positive, and since their product is 4, we have:
√ √
0<a−b d≤2 and 2 ≤ a + b d. (39)

We will now show that a must be positive and b must be nonnegative. From the inequal-
ities in (39), we have:
√ √
a−b d≤a+b d
√ √
−b d ≤ b d

0 ≤ 2b d
0≤b
√ √
Therefore,
√ b is nonnegative. Since 0 < a − b d, we have b d < a, and it follows that
0 ≤ b d < a. This implies that 0 < a, so a is positive.

6.4 Obtain a Contradiction


We will now obtain a contradiction to complete our proof.√ Combining √ the definition of a and
b from equation (38) with inequality (36), we have a+b d < x1 +y1 d. This does not satisfy
the conclusion of Theorem 3, so something in the hypothesis of the theorem must be false.
That is, it cannot be true that a, b, x1 , and y1 are positive integers for which a2 − db2 = 4,
x21 − dy12 = 4, and a > x1 . However, it is true that a2 − db2 = 4 and x21 − dy12 = 4, and a, x1 ,
and y1 are positive integers. We know that since a is a positive integer, then a > x1 by the
minimality of x1 . The only possibility is that b is not positive. Therefore, b = 0.

24
Since we previously showed that a2 − db2 = 4, we now see that a = 2. Thus, we have:

2=a+b d

√ (x1 − y1 d)m
2 = (u + v d)
2m√
√ (x1 − y1 d)m
4 = (u + v d) .
2m−1
However, it is also true that
√ √
(x1 − y1 d)m (x1 − y1 d)m
4= x2m − 2
dym = · .
2m−1 2m−1
Therefore, it follows that √

(x1 + y1 d)m
u+v d=
2m−1
√ √ √
and we can finally conclude that u + v d = xm + ym d by the definition of xn + yn d in
equation (35). In summary, we have proved the following theorem:
Theorem 12. If (x, y) = (u, v) is a positive integer solution of x2 − dy 2 = 4, with d ≡ 5
(mod 8), then there exists a positive integer m such that

√ (x1 + y1 d)m
u+v d=
2m−1
where (x1 , y1 ) is the fundamental solution of x2 − dy 2 = 4.
Proof. The proof is contained in Section 6.
Interestingly, the proof of Theorem 12 holds for any odd d, though we know from Theorem
7 that if x2 − dy 2 = 4 can be solved for odd integers x and y, then x ≡ 5 (mod 8).

7 Programming
In order to quickly find solutions to lots of Pell Equations, I wrote a computer program.
After the user enters values of d and k, my program checks many integers x in an attempt to
find one that makes y an integer. My program is somewhat more efficient than an exhaustive
search that tests all positive integers x; as discussed in Section 4.1, my program only tests
values of x whose square is congruent to k modulo d. Using such an exhaustive search, my
program can quickly solve Pell Equations that have solutions with x less than a few billion.
It is true that more sophisticated methods exist to find solutions to Pell Equations. One
clever method is credited to the eleventh-century Indian mathematician Bhaskara and is the
subject of a paper I wrote for Senior Math Seminar titled Bhaskara’s Method for Solving
Pell Equations [4]. The generally accepted method for solving Pell Equations today involves
continued fractions, but I have not studied it.

25
My program, PellSolver, has two modes: it can solve a single equation or many equations
at once. If the user chooses to solve a single equation, the program asks for values of d and
k, as well as a maximum value of x to test. The program identifies the Pell Equation as
unsolvable if k is a quadratic nonresidue modulo d. Otherwise, the program tests all values
of x whose square is congruent to k modulo d, stopping when it finds an integer solution
(x, y) or when x reaches the user-specified maximum. If the search is successful, the program
outputs the smallest positive integer solution to the Pell Equation, as well as the amount of
time it took to find the solution.
Solving many equations with a single command is useful if we wish to investigate patterns
in solutions while varying d, k, or both. For example, the solutions found to Pell Equations
where d ≡ 5 (mod 8) and k = 4 in Table 4 of Section 5.3 were found using PellSolver. If
the user chooses to solve many equations, the program asks for minimum and maximum
values of d. If these values are different, the program asks for a step by which to increment
d to obtain the next equation. For example, to solve x2 − dy 2 = 4 for values of d between 5
and 45 with d ≡ 5 (mod 8), the user would choose d-minimum as 5, d-maximum as 45, and
d-step as 8. The program allows the user to adjust k similarly, by specifying a minumum,
maximum, and a step. Lastly, the program asks for a maximum value of x as an upper limit
to test. For each specified value of k, the program then attempts to solve x2 − dy 2 = k for
each specified value of d. (That is, incrementing d and k is accomplished with nested loops,
with k being incremented in the outer loop and d in the inner loop.)
Sample output from PellSolver follows, with user input in bold:

PellSolver Menu:
1. Solve a single equation
2. Solve many equations
3. Quit
1
We will attempt to find solutions to equations of the form x^2- dy^2 = k
Enter a value for d:
46
Enter a value for k:
1
Enter a max value for x:
10000000
Solution: x = 24335, y = 3588 found in 0 ms

PellSolver Menu:
1. Solve a single equation
2. Solve many equations
3. Quit
1

26
We will attempt to find solutions to equations of the form x^2- dy^2 = k
Enter a value for d:
4
ERROR: d=4 is a perfect square

PellSolver Menu:
1. Solve a single equation
2. Solve many equations
3. Quit
2

Enter a min value for d:


50
Enter a max value for d:
55
Enter a step for d:
1
Enter a min value for k:
1
Enter a max value for k:
1
Enter a max value for x:
100000000
x^2 - 50y^2 = 1 -- Solution: x = 99, y = 14
x^2 - 51y^2 = 1 -- Solution: x = 50, y = 7
x^2 - 52y^2 = 1 -- Solution: x = 649, y = 90
x^2 - 53y^2 = 1 -- Solution: x = 66249, y = 9100
x^2 - 54y^2 = 1 -- Solution: x = 485, y = 66
x^2 - 55y^2 = 1 -- Solution: x = 89, y = 12
The search took 70 milliseconds.

The Java source code for my program is included in Appendix A.

27
8 Table of Solutions
Table 5 contains the fundamental solutions of the Pell Equations x2 − dy 2 = 1 for nonsquare
d < 100.
d x y d x y d x y
2 3 2 37 73 12 69 7775 936
3 2 1 38 37 6 70 251 30
5 9 4 39 25 4 71 3480 413
6 5 2 40 19 3 72 17 2
7 8 3 41 2049 320 73 2281249 267000
8 3 1 42 13 2 74 3699 430
10 19 6 43 3482 531 75 26 3
11 10 3 44 199 30 76 57799 6630
12 7 2 45 161 24 77 351 40
13 649 180 46 24335 3588 78 53 6
14 15 4 47 48 7 79 80 9
15 4 1 48 7 1 80 9 1
17 33 8 50 99 14 82 163 18
18 17 4 51 50 7 83 82 9
19 170 39 52 649 90 84 55 6
20 9 2 53 66249 9100 85 285769 30996
21 55 12 54 485 66 86 10405 1122
22 197 42 55 89 12 87 28 3
23 24 5 56 15 2 88 197 21
24 5 1 57 151 20 89 500001 53000
26 51 10 58 19603 2574 90 19 2
27 26 5 59 530 69 91 1574 165
28 127 24 60 31 4 92 1151 120
29 9801 1820 61 1766319049 226153980 93 12151 1260
30 11 2 62 63 8 94 2143295 221064
31 1520 273 63 8 1 95 39 4
32 17 3 65 129 16 96 49 5
33 23 4 66 65 8 97 62809633 6377352
34 35 6 67 48842 5967 98 99 10
35 6 1 68 33 4 99 10 1

Table 5: Fundamental solutions to x2 − dy 2 = 1 for nonsquare d < 100

28
References
[1] Barbeau, Edward J. Pell’s Equation. Springer, 2000.

[2] “Pell, John.” Dictionary of Scientific Biography. Vol 10, pg 495. New York: Charles
Scribner’s Sons.

[3] Vanden Eynden, Charles. Elementary Number Theory, second edition. McGraw Hill,
2001.

[4] Wright, Matthew. “Bhaskara’s Method for Solving Pell Equations.” 2006.

29
A Listing of PellSolver
1 /* class: PellSolver
2 * author: Matthew Wright
3 * date: 3 April 2006
4 * version: 6
5 */
6
7 import [Link].*;
8 import [Link].*;
9 import [Link].*;
10
11 class PellSolver
12 {
13 public static void main(String[] args) throws IOException
14 {
15 boolean repeat = true;
16 while(repeat)
17 {
18 [Link]("PellSolver Menu:\n1. Solve a single equation\n"+
19 "2. Solve many equations\n3. Quit");
20 BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
21 int choice = (new Integer([Link]())).intValue();
22 switch(choice)
23 {
24 case 1: solveOne();
25 break;
26 case 2: solveMany();
27 break;
28 default: repeat = false;
29 }
30 }//end while
31 }//end main()
32
33 //method to solve a single Pell Equation
34 public static void solveOne() throws IOException
35 {
36 //variables
37 long max;
38 boolean debug = false;
39
40 //Objects
41 BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
42 int d, k;
43
44 //get input
45 [Link]("We will attempt to find solutions to equations of the form"+
46 "x^2- dy^2 = k");
47 [Link]("Enter a value for d: ");
48 d = [Link]([Link]());
49

30
50 //d cannot be a square
51 if([Link]([Link](d)) - [Link](d) == 0)
52 {
53 [Link]("ERROR: d="+d+" is a perfect square\n\n");
54 return;
55 }
56
57 [Link]("Enter a value for k: ");
58 k = [Link]([Link]());
59 [Link]("Enter a max value for x: ");
60 max = [Link]([Link]());
61
62 //create a Pell object
63 Pell eq = new Pell(d, k, true);
64
65 //start timer
66 long beginTime = [Link]();
67
68 //look for a solution
69 Pair solution = null;
70 try
71 {
72 solution = [Link](0, max);
73 }
74 catch(QuadraticNonresidueException e)
75 {
76 [Link]("NO SOLUTION because "+k+" is a quadratic nonresidue mod "+d+"\n\n");
77 }
78
79 //stop timer
80 long timeElapsed = [Link]() - beginTime;
81
82 //do we have a solution?
83 if(solution == null)
84 {
85 [Link]("Max value reached; no solution found in "+timeElapsed+" ms\n\n");
86 }
87 else
88 {
89 [Link]("Solution: x = "+solution.x+", y = "+solution.y+" found in "+
90 timeElapsed+" ms\n\n");
91 }
92
93 }//end solveOne()
94
95 //method to solve many Pell Equations
96 public static void solveMany() throws IOException
97 {
98 //variables
99 int d1, //starting value for d
100 d2, //ending value for d

31
101 dd=1, //d step (default is 1)
102 k1, //starting value for k
103 k2, //ending value for k
104 dk=1, //k step (default is 1)
105 max; //max value for x
106
107 //use a BufferedReader for input
108 BufferedReader in = new BufferedReader(new InputStreamReader([Link]));
109
110 //input d
111 [Link]("\nEnter a min value for d: ");
112 d1 = [Link]([Link]());
113 [Link]("Enter a max value for d: ");
114 d2 = [Link]([Link]());
115 if(d1 != d2)
116 {
117 [Link]("Enter a step for d: ");
118 dd = [Link]([Link]());
119 }
120
121 //input k
122 [Link]("Enter a min value for k: ");
123 k1 = [Link]([Link]());
124 [Link]("Enter a max value for k: ");
125 k2 = [Link]([Link]());
126 if(k1 != k2)
127 {
128 [Link]("Enter a step for k: ");
129 dk = [Link]([Link]());
130 }
131
132 //input max
133 [Link]("Enter a max value for x: ");
134 max = [Link]([Link]());
135
136 //start timer
137 long beginTime = [Link]();
138
139 //loop k
140 for(;k1 <= k2; k1+=dk)
141 {
142 //loop d
143 for(int di = d1; di <= d2; di+=dd)
144 {
145 //make sure d is nonsquare
146 if([Link]([Link](di)) - [Link](di) == 0)
147 {
148 continue;
149 }
150
151 //create a Pell object

32
152 Pell eq = new Pell(di, k1, true);
153
154 //look for a solution
155 Pair solution = null;
156 try
157 {
158 solution = [Link](0, max);
159 //did we find a solution?
160 if(solution == null)
161 {
162 [Link](eq+" !! Max value reached; no solution found.");
163 }
164 else
165 {
166 [Link](eq+" -- Solution: x = "+solution.x+", y = "+solution.y);
167 }
168 }
169 catch(QuadraticNonresidueException e)
170 {
171 [Link](eq+" !! NO SOLUTION because "+k1+
172 " is a quadratic nonresidue mod "+di);
173 }
174 }//end loop d
175 }//end loop k
176
177 //stop timer
178 long timeElapsed = [Link]() - beginTime;
179 [Link]("The search took "+timeElapsed+" milliseconds.\n\n");
180 }//end solveMany()
181 }//end class PellSolver6
182
183 class Pell
184 {
185 //data members
186 int d, k;
187 boolean output;
188
189 //constructor
190 public Pell(int d, int k, boolean o)
191 {
192 this.d = d;
193 this.k = k;
194 [Link] = o;
195 }
196
197 //method to exhaustively search to find the smallest positive integer solution greater
198 // than start
199 //tests all values of x between start and start + max
200 //returns null if no solution is found
201 public Pair exhaustiveSearch(int start, long max)
202 {

33
203 boolean found = false;
204 for(long x = start; x <= start+max; x++)
205 {
206 //show output, if necessary
207 if(output && (x-start)%1000000 == 0)
208 {
209 [Link]("-testing x="+x);
210 }
211
212 //test this value of x
213 Pair xy = testX(x);
214 if(xy == null) //no solution
215 {
216 continue;
217 }
218 else //solution found!
219 {
220 return xy;
221 }
222
223 }//end for
224
225 //no solution found
226 return null;
227
228 }//end exhaustiveSearch()
229
230 //method to see if x is a solution to the Pell equation
231 //returns the solution as a Pair (x, y) if it is a solution
232 //returns null otherwise
233 public Pair testX(long x)
234 {
235 //**first, (x^2 - k)/d must be a positive integer, that is, (x^2 - k) must be positive
236 // and congruent to 0 mod d
237 if((x*x - k)%d != 0 || (x*x - k) <= 0)
238 {
239 return null;
240 }
241
242 //**second, (x^2 - k)/d must be a square
243 long maybeSquare = (x*x - k)/d;
244
245 //I think we might encounter with round-off error, so I prefer to work with integers
246 //---->Could we lose precision while converting a long to a double?
247 long rootTest = (long) [Link]([Link]((double) maybeSquare));
248 if(rootTest*rootTest == maybeSquare)
249 {
250 //we have a solution!
251 return new Pair(x, rootTest);
252 }
253 rootTest++;

34
254 if(rootTest*rootTest == maybeSquare)
255 {
256 //we have a solution!
257 return new Pair(x, rootTest);
258 }
259
260 //not a solution
261 return null;
262
263 }//end testX()
264
265 //method to intelligently search to find the smallest positive integer solution
266 //returns null if no solution is found
267 public Pair smartSearch(int start, long max) throws QuadraticNonresidueException
268 {
269 //if d is prime, then check to see if k is a quadratic residue mod d
270 if(dPrime())
271 {
272 //check to see if k is a quadratic residue mod d
273 int a = kPrimeQuadRes();
274
275 //if k is a quadratic residue mod d, then test values of x congruent to +/- a (mod d)
276 if(a != -1)
277 {
278 //for each base congruent to 0 (mod d) between start and start + max,
279 //test each x equal to (base - a) or (base + a)
280 //(this loop may test more values than strictly necessary at the endpoints)
281 for(long base = start - (start%d); base < start + max; base += d)
282 {
283 //test base - a
284 Pair xy;
285 if(base - a > start)
286 {
287 xy = testX(base - a);
288 if(xy != null)
289 {
290 //we have a solution!
291 return xy;
292 }
293 }
294
295 //test base + a
296 if(base + a > 0)
297 {
298 xy = testX(base + a);
299 if(xy != null)
300 {
301 //we have a solution!
302 return xy;
303 }
304 }

35
305
306 }//end for
307
308 //we did not find a solution
309 return null;
310
311 }
312 else //if k is a quadratic nonresidue mod d, then there is no solution
313 {
314 throw(new QuadraticNonresidueException());
315 }
316 }
317 else //d is not prime
318 {
319 //find quadratic residues
320 int[] a = kQuadRes();
321
322 //if there are quadratic residues, then test values of x congruent to them (mod d)
323 if([Link] > 0)
324 {
325 //for each base congruent to 0 (mod d) between start and start + max,
326 //test each x equal to base + (a quadratic residue)
327 //(this loop may test more values than strictly necessary at the endpoints)
328 for(long base = start - (start%d); base < start + max; base += d)
329 {
330 for(int i=0; i<[Link]; i++)
331 {
332 if(base + a[i] > 0)
333 {
334 Pair xy = testX(base + a[i]);
335 if(xy != null)
336 {
337 //we have a solution!
338 return xy;
339 }
340 }
341 }
342 }//end for
343
344 //we did not find a solution
345 return null;
346 }
347 else //then there are no quadratic residues, so there is no solution
348 {
349 throw(new QuadraticNonresidueException());
350 }
351 }
352 }//end smartSearch()
353
354
355 //method to determine whether d is prime

36
356 public boolean dPrime()
357 {
358 int sqrt = (int) [Link]([Link]((double) this.d));
359 for(int i=2; i<=sqrt; i++)
360 {
361 if(d%i == 0) //then i divides n
362 {
363 return false;
364 }
365
366 }
367
368 return true;
369 }//end dPrime()
370
371 //method to determine whether k is a quadratic residue mod d
372 //pre: d must be prime, to guarantee that there are exactly two solutions to a^2
373 // congruent to k (mod d)
374 // specifically, a and -a (mod d)
375 //post: returns the smallest nonnegative integer whose square is congruent to k mod d
376 // returns -1 if k is a quadratic nonresidue mod d
377 public int kPrimeQuadRes()
378 {
379 int kMod = k%d;
380 //kMod should not be negative
381 if(kMod<0)
382 kMod+=d;
383
384 for(int i=0; i<d; i++)
385 {
386 if((i*i)%d == kMod)
387 {
388 return i;
389 }
390 }
391 return -1;
392
393 }//end kQuadRes()
394
395 //method to find all integers (mod d) whose squares are congruent to k (mod d)
396 //this method will work even if k is composite
397 //post: returns an array of int
398 // if no such integers exist, returns an array of length 0
399 public int[] kQuadRes()
400 {
401 Vector v = new Vector();
402
403 int kMod = k%d;
404 //kMod should not be negative
405 if(kMod<0)
406 kMod+=d;

37
407
408 for(int i=0; i<d; i++)
409 {
410 if((i*i)%d == kMod)
411 {
412 [Link](new Integer(i));
413 }
414 }
415
416 //create array
417 int[] a = new int[[Link]()];
418 for(int i=0; i<[Link](); i++)
419 {
420 a[i] = ((Integer) [Link](i)).intValue();
421 }
422
423 return a;
424 }//end kQuadRes
425
426 //method to output this Pell as a string
427 public String toString()
428 {
429 return "x^2 - "+d+"y^2 = "+k;
430 }
431 }//end class Pell
432
433 class Pair
434 {
435 //data members
436 public long x, y;
437
438 //constructor
439 public Pair(long x, long y)
440 {
441 this.x = x;
442 this.y = y;
443 }
444 }//end class Pair
445
446 //class QuadraticNonresidueException
447 //thrown if k turns out to be a quadratic nonresidue mod d, in which case the Pell equation
448 // cannot have solutions
449 class QuadraticNonresidueException extends Exception
450 {
451 public QuadraticNonresidueException()
452 {
453 super();
454 }
455 }//end class QuadraticNonresidueException

38

You might also like