Understanding Pell's Equations
Understanding Pell's Equations
Matthew Wright
May 2, 2006
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
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
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.
k y
√ p
x
√k
Figure 1: Equations of the form x2 + py 2 = 1 have finitely many positive integer solutions
(x, y).
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
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
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].
√ 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)
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:
8
Multiplying the equations together, we have
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
But x21 − y12 = k and k ≡ 0 (mod k), so x21 − dy12 ≡ 0 (mod k). Therefore,
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
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.
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
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?
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 .
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:
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.
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:
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
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)
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
x y
5 1
23 5
110 24
527 115
2525 551
12098 2640
57965 12649
277727 60605
1330670 290376
6375623 1391275
Therefore, we see that solutions (xn , yn ) of x2n − dyn2 = 4n can be found recursively by
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
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
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.
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
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.
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
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.
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.
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
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
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