This article discusses solving the (generalized) Pell equation. A generalized Pell equation is an indeterminate equation in x and y of the form
x2−Dy2=N,
where D is a positive integer that is not a perfect square1, and N is a nonzero integer. The narrow sense of the Pell equation refers specifically to the cases N=1 or N=±1, and sometimes also includes N=±4. Generalized Pell equations are closely related to finding quadratic integers of norm N in the ring of real quadratic integers; the cases commonly called the (narrow) Pell equation can be viewed as finding units in that ring.
In this article, Pell equation specifically means the case N=1. Correspondingly, the case N=−1 is called the negative Pell equation2 (negative Pell's equation).
Structure of the solutions
The integer solutions (x,y) of a generalized Pell equation are closely connected with the quadratic integer x+yD, so solutions of Pell equations are often written in the form x+yD in the literature. The norm of a quadratic integer is
N(x+yD)=x2−Dy2,
so a generalized Pell equation is roughly the problem of finding quadratic integers of norm N. There is, however, a subtle difference. When x and y are both integers, x+yD is certainly a quadratic integer; conversely, a quadratic integer does not necessarily require x and y to be integers—in the case D≡1(mod4), x and y may also both be half-integers3.
This distinction is especially important when finding fundamental units. A unit in a quadratic integer ring is a quadratic integer of norm ±1. For D≡2,3(mod4), finding such units only requires solving the generalized Pell equation for N=±1; for D≡1(mod4), the cases N=±4 must also be considered. The method for finding units will be discussed below.
To understand the structure of generalized Pell equation solutions, begin with the Brahmagupta identity:
Using this identity, integer solutions of x2−Dy2=N1 and x2−Dy2=N2 can be combined to obtain an integer solution of x2−Dy2=N1N2. From the viewpoint of quadratic integers, combining solutions is simply multiplication of quadratic integers, which illustrates why writing Pell equation solutions in quadratic-integer form is convenient. In particular, taking N1=N and N2=1 shows that if one solution of x2−Dy2=N and all solutions of the corresponding Pell equation x2−Dy2=1 are known, more solutions of x2−Dy2=N can be obtained. This method need not generate all solutions, but it at least shows that understanding the structure of Pell equation solutions is important for understanding the structure of generalized Pell equation solutions.
Pell equation
Geometrically, the equation x2−Dy2=1 is a hyperbola whose real axis is the x-axis and imaginary axis is the y-axis. Every point on the hyperbola corresponds uniquely to a nonzero value of x+yD: the left branch corresponds to negative values and the right branch to positive values. Moreover, on each branch, the values of x+yD increase strictly from bottom to top. The values of quadratic integers therefore give the solutions of the Pell equation a natural order in x+yD.
The hyperbola is symmetric about both the x-axis and the y-axis, so it is enough to consider the part in the first quadrant; the other solutions follow by symmetry. Equivalently, consider only solutions with x+yD>1. If the equation has a nontrivial solution besides (±1,0), then there is a solution (x1,y1) in the first quadrant for which x+yD is minimal. It is also the lattice point in the first quadrant, excluding the coordinate axes, with both coordinates minimal, and is called the fundamental solution of the Pell equation4. By the discussion above, every integer pair (xk,yk) satisfying xk+ykD=(x1+y1D)k is a Pell equation solution in the first quadrant. Conversely, these are exactly all Pell equation solutions in the first quadrant. Symmetry then gives the following conclusion:
Theorem
Let (x1,y1) be the fundamental solution of x2−Dy2=1. Then all its solutions are
{(x,y):x+yD=±(x1+y1D)k,k∈Z}.Proof
First prove that there are no other solutions in the first quadrant. Suppose there is another solution x+yD and, for some k≥0,
xk+ykD<x+yD<xk+1+yk+1D.
Geometrically, this means that the lattice point (x,y) lies strictly between (xk,yk) and (xk+1,yk+1) on the hyperbola. Multiplying the inequality by xk−ykD=(xk+ykD)−1 gives
By the monotonicity mentioned above, this says that (xxk−Dyyk,xky−xyk) is an integer point between (1,0) and (x1,y1), contradicting the choice of (x1,y1).
When extending the first-quadrant solutions to the whole plane, replacing k by its opposite (taking the entire expression's inverse) gives reflection about the x-axis, while negating the entire expression gives reflection about the origin. Adding the trivial solution for k=0 gives all Pell equation solutions.
The preceding discussion only assumed that a fundamental solution exists. We now show that the Pell equation always has a nontrivial solution.
Theorem
The Pell equation x2−Dy2=1 always has an integer solution other than (±1,0).
Proof
First, Dirichlet's theorem states that there are infinitely many pairs of positive integers (x,y) satisfying
yx−D≤y21
and all of them satisfy
∣x2−Dy2∣=y2yx−Dyx+D≤y21+2D<1+2D.
Hence there is an integer m∈(−1−2D,1+2D) such that infinitely many pairs (x,y) satisfy x2−Dy2=m. Classify these (x,y) by their residues modulo m. For some pair of integers (x0,y0), infinitely many (x,y) must satisfy x≡x0(modm) and y≡y0(modm). Choose two distinct pairs (x1,y1) and (x2,y2) satisfying these conditions. Then
so the right-hand side is an integer quadratic integer. Since (x1,y1)=(x2,y2), it is nontrivial. Thus the Pell equation indeed has a nontrivial solution.
The proof in this section is nonconstructive. In the discussion of solving Pell equations below, we directly use convergents of continued fractions to construct a Pell equation solution, providing another proof that a nontrivial solution exists. Although the structure of the Pell equation solutions obtained here agrees with the structure of units in the ring of real quadratic integers, for D≡1(mod4) this section does not completely resolve the structure of units in the corresponding ring; that will be discussed below.
Generalized Pell equation
The graph of the generalized Pell equation x2−Dy2=N is also a hyperbola in the plane, symmetric about both the x-axis and the y-axis. As noted above, some solutions of the equation x2−Dy2=N may differ only by a factor that is a Pell equation solution, so the solutions of x2−Dy2=N can be divided into equivalence classes. For two solutions (x1,y1) and (x2,y2) of x2−Dy2=N, if there is a Pell equation solution (u,v) such that x2+y2D=(x1+y1D)(u+vD), then the solutions (x1,y1) and (x2,y2) are called equivalent. The necessary and sufficient conditions for two solutions to be equivalent are
N∣(x1x2−Dy1y2),N∣(x2y1−x1y2).
Since Pell equation solutions are relatively easy to find, a natural idea is to find one solution in each equivalence class. Once these solutions are known, all solutions of the generalized Pell equation can be obtained using the corresponding Pell equation solutions. Within the equivalence classes of generalized Pell equation solutions, symmetry guarantees a solution with nonnegative and as small as possible y: if it is unique, it is called the fundamental solution of that class; otherwise, the class has two solutions with nonnegative minimal y, symmetric about the y-axis, and the one with x>0 is chosen as the fundamental solution. Thus solving x2−Dy2=N amounts to finding its set U of fundamental solutions. Let (r,s) be the fundamental solution of its corresponding Pell equation. Then all solutions of the generalized Pell equation are
{(x,y):x+yD=±(r+sD)k(u+vD),k∈Z,u+vD∈U}.
There are only finitely many fundamental solutions of a generalized Pell equation. Indeed, the general solution above shows that ∣u+vD∣ must lie between r−sD and r+sD. The references at the end give sharper estimates for the ranges of the coordinates of fundamental solutions. Unlike the Pell equation, the generalized Pell equation may have no solution.
Besides combining solutions, all solutions in the same equivalence class can be obtained from a generalized Pell solution (u,v) and the fundamental Pell solution (r,s) by the recurrence
xk=2rxk−1−xk−2,yk=2ryk−1−yk−2,
where xk+ykD=(r+sD)k(u+vD). This is because xn and yn can each be written, for some real pair (A,B), in the form A(r+sD)k+B(r−sD)k. By Vieta's theorem, r±sD are the two real roots of x2−2rx+1=0, so xn and yn both satisfy the second-order constant-coefficient recurrence above. Compared with combining solutions, this recurrence uses fewer multiplications.
Methods of solution
Both Pell equations and generalized Pell equations can be solved using continued fractions.
The PQa algorithm
The algorithms discussed here are all based on the PQa algorithm, which can find the continued-fraction expansion of a particular quadratic irrational.
Let integers P0,Q0,D satisfy Q0=0, D>0 and not a perfect square, and P02≡D(modQ0). Then the continued-fraction expansion of the quadratic irrational
ω=Q0P0+D
can be found by the following recurrence, with notation [a0,a1,⋯]:
The numerator and denominator Ak and Bk of the k-th convergent of ω are then given by the following recurrence:
Ak=akAk−1+Ak−2,Bk=akBk−1+Bk−2
with A−1=1, A−2=0, B−1=0, and B−2=1.
The correctness of these formulas was proved in the article on continued fractions. It also explains that because a quadratic irrational has a periodic continued fraction, the triple (Pk,Qk,ak) eventually enters a cycle, so the algorithm always terminates in finitely many steps. Let the minimum period length be ℓ, and let the earliest starting position of the cycle be k0. Then the continued-fraction expansion is
ω=[a0,⋯,ak0−1,ak0,⋯,ak0+ℓ−1].
To solve Pell equations with the PQa algorithm, we need the following result:
Theorem
Continue with the notation above. Let Gk=Q0Ak−P0Bk. Then the integer pair (Gk−1,Bk−1) satisfies
Gk−12−DBk−12=(−1)kQ0Qk,
and their greatest common divisor gcd(Gk−1,Bk−1) divides Qk.
Proof
Let the k-th remainder (complete quotient) in the continued-fraction expansion of ω be ωk, so
Cancel the denominators on both sides, compare the coefficients of the rational and irrational parts, and substitute the expression for Gk. This gives
This result gives a way to find solutions of x2−Dy2=N. Choose Q0>0 appropriately and choose P0 as a solution of the congruence P02≡D(modQ0). Run the PQa algorithm on (P0+D)/Q0 until (−1)kQ0Qk=N is found. Then (Gk−1,Bk−1) is a solution of the original equation. Moreover, if Qk=±1, this solution is primitive, meaning that Gk−1 and Bk−1 are coprime.
This idea is the core of solving Pell equations and generalized Pell equations. With it understood, we can handle some algorithmic details and prove that all solutions can be obtained this way.
Pell equation
To solve x2−Dy2=1, run the PQa algorithm from (P0,Q0,D)=(0,1,D) until (−1)kQk=1. Then (Ak−1,Bk−1) is a Pell equation solution, because at this point Gk−1 is exactly Ak−1. For the Pell equation, this process can be described more precisely.
First, the solution must occur at the end of a period. The process above is the continued-fraction expansion of D. We have the following result:
D=[⌊D⌋,a1,⋯,aℓ−1,2⌊D⌋].
The period length is ℓ, and it starts at term 1 (with indices starting at 0). The remainder at term ℓ is ⌊D⌋+D, which shows that Qℓ=1. Therefore, if ℓ is even, (Aℓ−1,Bℓ−1) is a nontrivial Pell equation solution; if ℓ is odd, (A2ℓ−1,B2ℓ−1) is a nontrivial Pell equation solution.
We next show that this solution is the fundamental solution. There are two reasons. First, the fractions x/y corresponding to all positive integer Pell solutions (x,y) occur among the convergents of D, so (x,y) must be some (Ak,Bk) during the PQa process. Second, Qk=1 does not occur at any other position besides the end of a period, because the recurrences for Ak and Bk make them increase with the index. Thus the smallest positive integer solution, the fundamental solution, must occur at the position just specified. These two facts follow from the following two theorems:
Theorem
Suppose x2−Dy2=N has a positive integer solution (x,y). If ∣N∣<D, then yx is a convergent of D.
When N<0, x>yD no longer holds. Instead, consider a solution of y2−D1x2=−DN. Since D∣N∣<D1, the preceding argument still applies. Thus xy is a convergent of D1. By the reciprocal theorem, yx is also a convergent of D.
Theorem
During the PQa algorithm above starting from (P0,Q0,D)=(0,1,D), Qk=1 implies ℓ∣k.
Proof
In the continued-fraction expansion of D, every remainder other than the 0th is a purely periodic continued fraction. Let Qk=1. By Galois's result, the remainder must satisfy ωk=Pk+D>1 and its conjugate −1<Pk−D<0, which means Pk=⌊D⌋. Therefore ωk equals ωℓ. Repetition of a remainder means that the continued fraction has entered its cycle. If k were not a multiple of ℓ, this would contradict the minimality of ℓ. Hence ℓ∣k.
Combining the discussion in this section, expanding D as a continued fraction—that is, running the PQa algorithm from (P0,Q0,D)=(0,1,D)—reaches the end of the first period when Qℓ=1 is first obtained. If ℓ is even, (Aℓ−1,Bℓ−1) is the fundamental solution; otherwise (A2ℓ−1,B2ℓ−1) is the fundamental solution. When ℓ is odd, it is unnecessary to continue the PQa algorithm to twice the period: we will soon see that A2ℓ−1+B2ℓ−1D=(Aℓ−1+Bℓ−1D)2, so the fundamental solution can be computed directly from (Aℓ−1,Bℓ−1). All other Pell equation solutions can be computed from the fundamental solution.
Example
Solve x2−14y2=1.
Running the PQa algorithm from (P0,Q0,D)=(0,1,14) gives the following results (the red entries form the first period):
k
P
Q
a
A
B
G
G2−DB2
0
0
1
3
3
1
3
−5
1
3
5
1
4
1
4
2
2
2
2
2
11
3
11
−5
3
2
5
1
15
4
15
1
4
3
1
6
101
27
101
−5
5
3
5
1
116
31
116
2
The period length ℓ=4 is even. The smallest positive integer solution is (G3,B3)=(15,4).
Solve x2−41y2=1.
Running the PQa algorithm from (P0,Q0,D)=(0,1,41) gives the following results (the red entries form the first period):
k
P
Q
a
A
B
G
G2−DB2
0
0
1
6
6
1
6
−5
1
6
5
2
13
2
13
5
2
4
5
2
32
5
32
−1
3
6
1
12
397
62
397
5
4
6
5
2
826
129
826
−5
5
4
5
2
2049
320
2049
1
6
6
1
12
25414
3969
25414
−5
7
6
5
2
52877
8258
52877
5
The period length ℓ=3 is odd. The smallest positive integer solution is (G5,B5)=(2049,320). It can also be computed from (G2,B2)=(32,5):
(32+541)2=2049+32041.
Negative Pell equation
By the discussion in the preceding section, solutions of the negative Pell equation also correspond to convergents of D, and can occur only when (−1)kQk=−1. This can happen only at the end of a period. Therefore, the negative Pell equation has a solution if and only if the period length ℓ is odd. When a solution exists, (Aℓ−1,Bℓ−1) is its fundamental solution, and it is solved in the same way as in the preceding section.
Using an argument similar to the proof of the structure of Pell equation solutions, we can prove:
Theorem
Suppose x2−Dy2=−1 has a solution with fundamental solution (x1,y1). Then all integer solutions of x2−Dy2=±1 belong to
{(x,y):x+yD=±(x1+y1D)k,k∈Z}.
In particular, the integer solution (x2,y2) satisfying x2+y2D=(x1+y1D)2 is the fundamental solution of x2−Dy2=1.
Proof
By symmetry, it is enough to consider positive integer solutions (x,y), that is, x+yD>1. However, since x2−Dy2=±1 consists of two hyperbolas, x+yD does not correspond one-to-one with (x,y). To handle this difficulty, first prove that the above (x2,y2) is the fundamental solution of x2−Dy2=1.
Clearly, (x2,y2) solves x2−Dy2=1. If (z,w) is the fundamental solution of x2−Dy2=1, then 1<z+wD≤x2+y2D. If the right inequality is strict, divide the inequality by x1+y1D to obtain −x1+y1D<(z+wD)(−x1+y1D)<x1+y1D. Expanding the middle expression gives x′+y′D, whose norm is −1, and (x′,y′) is also an integer solution. Taking the reciprocal of this inequality shows that −x′+y′D also lies between −x1+y1D and x1+y1D. The quadratic integers ±x′+y′D are reciprocals of each other, so one of them must be greater than 1. But no other quadratic integer of norm −1 should occur between 1 and x1+y1D, contradicting the minimality of x1+y1D. Thus x2+y2D=z+wD, and (x2,y2) is the fundamental solution of x2−Dy2=1.
Based on this, if a solution of x2−Dy2=±1 were not of the form (x1+y1D)k, there would be a k such that (x1+y1D)2k<x+yD<(x1+y1D)2k+2. Canceling the factor (x1+y1)2k+1 would produce a quadratic integer x′+y′D=1 of norm ±1 between −x1+y1D and x1+y1D. Repeating the reciprocal argument from the preceding paragraph gives a contradiction to the minimality of x1+y1D. The proposition follows.
Since (Aℓ−1,Bℓ−1) is the smallest positive integer solution of the negative Pell equation, all positive integer solutions of x2−Dy2=±1 lie in
{(x,y):x+yD=(Aℓ−1+Bℓ−1D)k,k∈N+}
and these positive integer solutions necessarily correspond to the convergents of D immediately before the end of a period. Since the numerators and denominators of convergents increase strictly, for every k∈N+ we have
(Aℓ−1+Bℓ−1D)k=Akℓ−1+Bkℓ−1D.
Among these positive integer solutions, odd k gives negative Pell solutions and even k gives Pell solutions; they alternate.
Determining whether the negative Pell equation has a solution requires computing the period length of the continued-fraction expansion of D, which is not easy. It is therefore desirable to find a simpler test. At present, however, there is no simple and easily computable criterion5. We give only one simple result here.
Theorem
If x2−Dy2=−1 has a solution, then 4 does not divide D and D has no prime factor of the form 4k+3. Conversely, if D=2 or D is a prime of the form 4k+1, then the equation necessarily has a solution.
Proof
First, a solution of the negative Pell equation means that −1 is a quadratic residue modulo D, and therefore −1 is also a quadratic residue modulo every factor d of D. Thus d=4 and d is not a prime of the form 4k+3. Conversely, x2−2y2=−1 has the nontrivial solution (1,1). It remains to handle the case where D is a prime of the form 4k+1.
Let D be a prime of the form 4k+1. We prove that x2−Dy2=−1 has a solution by starting with the fundamental solution pair (u,v) of x2−Dy2=1 and constructing a solution (α,β) of x2−Dy2=−1. If u were even, reducing u2−Dv2=1 modulo 4 would give v2≡−1(mod4), but −1 is not a quadratic residue modulo 4. Hence u is odd. Consider Dv2=u2−1=(u+1)(u−1). Since u is odd, gcd(u+1,u−1)=gcd(u+1,2)=2. Therefore, distributing the factors of Dv2 between u+1 and u−1, one must be 2α2 and the other 2Dβ2, where α and β are coprime positive integers and v=2αβ. Substituting u=α2+Dβ2 and v=2αβ into u2−Dv2=1 gives α2−Dβ2=±1. Since (u,v) is the fundamental Pell solution and (α,β) is a smaller positive integer pair than (u,v), the right-hand side cannot be +1, so it must be −1. Thus x2−Dy2=−1 has the solution (α,β).
If D is composite, having no prime factor of the form 4k+3 and having no square factor still does not guarantee a solution to x2−Dy2=−1; for example, x2−34y2=−1 has no solution.
Example
From the calculations in the preceding examples, x2−14y2=−1 has no solution, while the smallest positive integer solution of x2−41y2=−1 is (G2,B2)=(32,5).
The case of norm ±4
We now discuss solutions of x2−Dy2=±4. Their behavior depends on Dmod4.
Some cases are easy. If D≡0(mod4), then x is even, so (x/2,y) solves u2−(D/4)v2=±1. In the remaining cases, x,y must be both odd or both even. If both are odd, reducing both sides modulo 4 gives D≡1(mod4). Thus, if D≡2,3(mod4), x,y must both be even, and (x/2,y/2) solves u2−Dv2=±1. Therefore, except when D≡1(mod4), solutions of x2−Dy2=±4 can be obtained from the corresponding (negative) Pell equation solutions for x,y.
Now consider D≡1(mod4), which cannot be reduced directly to a case already solved. To find the fundamental solution, apply the PQa algorithm to (P0,Q0,D)=(1,2,D). When Qℓ=2 is first obtained, the end of the first period has been reached. If the period length l is even, (Gℓ−1,Bℓ−1) is the fundamental solution of x2−Dy2=4; otherwise the pair (Gℓ−1,Bℓ−1) is the fundamental solution of x2−Dy2=−4. Starting from the pair (Gℓ−1,Bℓ−1), all solutions of x2−Dy2=±4 are given by the following expression:
⎩⎨⎧(x,y):2x+yD=±(2Gℓ−1+Bℓ−1D)k,k∈Z⎭⎬⎫.
If the period length ℓ is even, all of these solve x2−Dy2=4; otherwise, when k is odd, (x,y) solves x2−Dy2=−4, and when k is even, (x,y) solves x2−Dy2=4.
The correctness of this algorithm depends on the following fact:
Theorem
Suppose x2−Dy2=±4 has a positive integer solution (x,y). If D≡1(mod4), then y(x+y)/2 is a convergent of 21+D.
Proof
First note that x,y must have the same parity, so (x+y)/2 is an integer. If (x,y) solves x2−Dy2=4, then x>yD>2y, and hence
If (x,y) solves x2−Dy2=−4, it is enough to prove 4y<x+yD to establish the same inequality. This holds for all cases except D=5,13. For D=5,13, substituting x=Dy2−4 shows that the inequality is equivalent to 2(D−2)y2>1. Apart from (D,y)=(5,1), this holds for all D=5,13 and positive integers y. In the remaining case (D,y)=(5,1), the solution of x2−5y2=−4 is (x,y)=(1,1), and we only need to verify that 11 is a convergent of 21+5=[1], which is immediate.
Theorem
Let D be a positive nonsquare integer. The continued-fraction expansion of the quadratic irrational ω=21+D has the form
ω=[⌊ω⌋,a1,⋯,aℓ−1,2⌊ω⌋−1],
where ℓ is the period length and ak=aℓ−k for every 1<k<ℓ.
Proof
Since ⌊ω⌋−1+ω>1 and its conjugate is ⌊ω⌋−ω, which lies between −1 and 0, Galois's result shows that ⌊ω⌋−1+ω is a purely periodic continued fraction:
⌊ω⌋−1+ω=[2⌊ω⌋−1,a1,⋯,aℓ−1].
Galois's result about the reciprocal of the negative conjugate gives
ω−⌊ω⌋1=[aℓ−1,⋯,a1,2⌊ω⌋−1].
Therefore, by the definition of continued fractions,
The uniqueness of continued-fraction expansions gives ak=aℓ−k for all 1<k<ℓ, proving the claimed expansion.
Theorem
Let D≡1(mod4). During the PQa algorithm above starting from (P0,Q0,D)=(1,2,D), Qk=2 implies ℓ∣k.
Proof
In the continued-fraction expansion of 21+D, every remainder other than the 0th is a purely periodic continued fraction. Let Qk=2. By Galois's result, the conjugate of ωk=2Pk+D satisfies −1<2Pk−D<0, or equivalently D−2<Pk<D. Since Qk∣Pk2−D always holds in the PQa algorithm (see the proof of algorithm correctness), Pk must be odd. Thus Pk is uniquely determined, namely Pk=P0+2(⌊ω⌋−1), so ωk=ωℓ. Repetition of a remainder means that the continued fraction has entered its cycle. If k were not a multiple of ℓ, this would contradict the minimality of ℓ. Hence ℓ∣k.
Theorem
Let (x1,y1) be the smallest positive integer solution of x2−Dy2=±4. Then all its solutions are
⎩⎨⎧(x,y):2x+yD=±(2x1+y1D)k,k∈Z⎭⎬⎫.Proof
By symmetry, it is enough to consider positive integer solutions (x,y). The only thing to prove is that every pair (x,y) in the set is indeed an integer solution of x2−Dy2=±4. The rest repeats the proof of the solution structure for x2−Dy2=±1.
Specifically, for any two integer solutions (x1,y1) and (x2,y2) of x2−Dy2=±4, define a positive real pair (x3,y3) by
2x3+y3D=2x1+y1D2x2+y2D.
Expanding the right side and comparing the rational and irrational coefficients gives
Thus x3 and y3 are integers. Multiplicativity of the norm then shows that (x3,y3) solves x2−Dy2=±4.
Combining these facts and repeating the arguments of the preceding sections proves the correctness of the algorithm for x2−Dy2=±4. These results show that x2−Dy2=±4 has a simple solution structure similar to that of x2−Dy2=±1: all solutions can be represented by its smallest positive integer solution, without solving any other equation.
In fact, all solutions of x2−Dy2=±1 can be found among the solutions of x2−Dy2=±4, so in this sense x2−Dy2=±4 is more fundamental. Clearly, (x,y) solves x2−Dy2=±1 if and only if (2x,2y) solves x2−Dy2=±4. The preceding analysis shows that when D≡2,3(mod4), every solution of x2−Dy2=±4 is even in both coordinates, and therefore corresponds to a solution of x2−Dy2=±1.
When D≡0(mod4), a solution (x,y) of x2−Dy2=±4 always has even x, but y may be odd. If y1 is even in the smallest positive solution (x1,y1), then y is even for every solution, and these integer solutions correspond one-to-one with the integer solutions of x2−Dy2=±1. If y1 is odd, the parity of yk agrees with k and alternates, so only even k correspond to solutions of x2−Dy2=±1. If y1 is odd in the smallest positive solution (x1,y1) of x2−Dy2=±4 and the norm of x1+y1D is −4, then for such D, the equation x2−Dy2=±4 has the solution x2−Dy2=−4, but x2−Dy2=−1 does not.
When D≡1(mod4), a solution (x,y) of x2−Dy2=±4 may be both odd or both even. If the smallest positive solution (x1,y1) is already even in both coordinates, then all its integer solutions are also even in both coordinates, and therefore correspond to integer solutions of x2−Dy2=±1. If the smallest positive solution (x1,y1) is odd in both coordinates, we have the following result:
Theorem
Let (x1,y1) be the smallest positive integer solution of x2−Dy2=±4. If x1 and y1 are both odd, then D≡5(mod8), and an integer solution (x,y) of this equation is even in both coordinates if and only if
2x+yD=±(2x1+y1D)3k,k∈Z.Proof
Reducing both sides of x12−Dy12=±4 modulo 8 gives D≡5(mod8). To prove the second conclusion, first show that (x3,y3) are both even. Since
Hence x3,y3 are both even. Therefore, for every k∈Z,
2x+yD=±(2x1+y1D)3k=±(2x3+y3D)k∈Z,
so (x,y) is even in both coordinates. Conversely, for r=1,2,
±(2x1+y1D)3k+r=±(2x3+y3D)k(2xr+yrD).
To prove that the corresponding (x,y) is not even, it is enough to prove that the expression is not integral, that is, that the second factor in the product is not integral. For r=1, this is the given condition. For r=2,
and x12+Dy12≡1+1×1=2(mod4), while x1y1≡1(mod2), so this expression is also not integral. Thus only powers divisible by 3 give solutions that are even in both coordinates.
In other words, one out of every three solutions of x2−Dy2=±4 is even in both coordinates, and corresponds to an integer solution of x2−Dy2=±1. This also shows that, for D≡1(mod4), x2−Dy2=−4 has a solution if and only if x2−Dy2=−1 has a solution.
The discussion so far is enough to compute the fundamental units of a real quadratic integer ring. Let D be a positive integer without a square factor. For D≡2,3(mod4), it is enough to find the smallest positive integer solution of x2−Dy2=±1; for D≡1(mod4), it is enough to find the smallest positive integer solution of x2−Dy2=±4. Given the smallest positive solution (x,y), the fundamental units are ±x±yD for D≡2,3(mod4), and 2±x±yD for D≡1(mod4).
Example
Solve x2−14y2=±4.
The preceding example shows that the smallest positive integer solution of x2−14y2=4 is (30,8), while x2−14y2=−4 has no solution.
Solve x2−41y2=±4.
Running the PQa algorithm from (P0,Q0,D)=(1,2,41) gives the following results (the red entries form the first period):
k
P
Q
a
A
B
G
G2−DB2
0
1
2
3
3
1
5
−16
1
5
8
1
4
1
7
8
2
3
4
2
11
3
19
−8
3
5
4
2
26
7
45
16
4
3
8
1
37
10
64
−4
5
5
2
5
211
57
365
16
6
5
8
1
248
67
429
−8
7
3
4
2
707
191
1223
8
8
5
4
2
1662
449
2875
−16
9
3
8
1
2369
640
4098
4
10
5
2
5
13507
3649
23365
−16
11
5
8
1
15876
4289
27463
8
The period length ℓ=5 is odd. The minimal positive integer solution of x2−41y2=−4 is (G4,B4)=(64,10), and the minimal positive integer solution of x2−41y2=4 is (G9,B9)=(4098,640). They satisfy:
24098+64041=(264+1041)2.
Of course, because D≡1(mod8), the conclusions above show that the minimal positive integer solutions of x2−41y2=±4 in this case are always even, and are always the minimal positive integer solutions of x2−41y2=±1 multiplied by 2. Thus, the example above can also be used directly.
Solve the equation x2−13y2=±4.
Running the PQa algorithm with (P0,Q0,D)=(1,2,13) gives the following result (the red portion is the first period):
k
P
Q
a
A
B
G
G2−DB2
0
1
2
2
2
1
3
−4
1
3
2
3
7
3
11
4
2
3
2
3
23
10
36
−4
3
3
2
3
74
33
119
4
The period length ℓ=1 is odd. The minimal positive integer solution of x2−13y2=−4 is (G0,B0)=(3,1), and the minimal positive integer solution of x2−13y2=4 is (G1,B1)=(11,3).
Because the minimal positive integer solutions of this equation are all odd, the pair (G2,B2)=(36,10) at the end of the third period can be used to obtain the corresponding (negative) Pell equation x2−13y2=±1 with minimal positive integer solution (18,5). It can also be obtained directly:
236+1013=(23+13)3.
Moreover, this is a solution of the negative Pell equation. The minimal positive integer solution of the corresponding Pell equation is (649,180).
Solve the equation x2−52y2=±4.
Because the minimal positive integer solutions of x2−13y2=±1 are (18,5) and (649,180), the minimal positive integer solutions of x2−52y2=±4 are (36,10) and (1298,360).
General case
Finally, we discuss how to solve the generalized Pell equation.
For the case ∣N∣<D, there is a simple solution. The conclusions above show that the equation x2−Dy2=N has a solution (x,y), which must satisfy that yx is a convergent of D. Moreover, from the structure of solutions discussed above, every fundamental solution (x,y) satisfies x+yD no greater than the corresponding fundamental solution of the Pell equation x2−Dy2=1, namely x1+y1D. By the monotonicity of the denominator sequence Bk in the PQa algorithm, these fundamental solutions of the generalized Pell equation must occur before the fundamental solution of the corresponding Pell equation. Therefore, run the PQa algorithm with (P0,Q0,D)=(0,1,D) until Qℓ′=1 and ℓ′ is even. During this process, for every (Ak,Bk) that appears, check whether there is an integer f such that
Ak2−DBk2=(−1)k+1Qk+1=N/f2
If this holds, record (fAk,fBk) as a minimal positive integer solution. All (fAk,fBk) recorded in this process are exactly the minimal positive integer solutions of x2−Dy2=N. Using (Aℓ′−1,Bℓ′−1), the fundamental solution of the corresponding Pell equation, we can generate all solutions of the generalized Pell equation from these minimal positive integer solutions. Note that, depending on whether the period length ℓ is even or odd, the ℓ′ above may be either ℓ or 2ℓ.
For a more general N, the above method no longer applies. First enumerate all square factors of N, writing each as f2; let m=N/f2, and enumerate all solutions to the congruence z2≡D(mod∣m∣) satisfying −∣m∣/2<z≤∣m∣/2, denoting each solution by z. Then run the PQa algorithm with (P0,Q0,D)=(z,∣m∣,D) until Qk=±1 or one period has ended. In the second case, the equation associated with this pair (f,z) has no solution. In the first case, further determine whether (−1)kQk=N/∣N∣. If the signs agree, (fGk−1,fBk−1) is a solution of x2−Dy2=N. Otherwise, it is a solution of x2−Dy2=−N, and it can be composed with the fundamental solution of the corresponding negative Pell equation to obtain a solution of x2−Dy2=N if and only if that negative Pell equation has a solution. After traversing all pairs (f,z), we obtain exactly one solution from each equivalence class of x2−Dy2=N, and that solution is the fundamental solution or minimal positive integer solution in its class. These solutions and the fundamental solution of the corresponding Pell equation can be used to generate all integer solutions of the equation. This algorithm is called the Lagrange–Matthews–Mollin algorithm.
The correctness of this algorithm follows from the theorem below:
Theorem
Suppose the equation x2−Dy2=N has an integer solution (x,y) with x≥0,y>0,gcd(x,y)=1. Let Q0=∣N∣, so gcd(Q0,y)=1. Let P0 be a solution of the congruence x≡−P0y(modQ0) with −Q0/2<P0≤Q0/2, and let the integer X satisfy x=Q0X−P0y. Then, P02≡D(modQ0), and yX is a convergent of ω=Q0P0+D, namely Bk−1Ak−1, with Qk=(−1)k∣N∣N.
Proof
Using x≡−P0y(modQ0) and x2−Dy2=N≡0(modQ0); it is clear that P02≡D(modQ0). Therefore,
We now prove that QP is a convergent of ω. Without loss of generality, suppose QP has continued fraction expansion
QP=[a0,a1,⋯,ak]
and PS−QR=(−1)k−1. Suppose qkpk is its k-th convergent. Then (pk,qk)=(P,Q), and the difference formula for convergents shows that pkqk−1−qkpk−1=(−1)k−1. This shows that
pk(S−qk−1)=qk(R−pk−1).
Consider the following cases:
If S=0, it is easy to verify Q=R=1, so ω=P+ζ−1=[P,ζ], and therefore QP=P is a convergent of ω with index 0;
If Q=S>0, then Q=S=1 and P−R=±1. In this case,
If P=R+1, then ω=R+1+ζ−11=[R,1,ζ], so QP=1R+1=[R,1] is a convergent of ω with index 1;
If P=R−1, then ω=R−1+1+ζ1=[R−1,ζ−1], so QP=R−1 is a convergent of ω with index 0;
If Q=S>0, then, since Q=qk∣(S−qk−1), there is always an integer κ such that S=κqk+qk−1 and R=κpk+pk−1. Since qk≥qk−1 and S>0, we have κ≥0. Therefore, ω=(κ+ζ)qk+qk−1(κ+ζ)pk+pk−1=[a0,a1,⋯,ak,κ+ζ], so QP is its k-th convergent.
In summary, yX is a convergent of ω=Q0P0+D, denoted by Bk−1Ak−1. Since Ak−12−DBk−12=(−1)kQ0Qk, so Qk=(−1)k∣N∣N.
The theorem guarantees that all positive solutions of the equation occur among the convergents of the corresponding quadratic irrational. When convergents are computed with the PQa algorithm, once the period is reached, the convergents are guaranteed to be positive. Therefore, by enumerating all quadratic irrationals allowed by the conditions of the theorem and computing their convergents through one period, we can find a solution. Since two solutions appearing among the convergents of the same quadratic irrational must be equivalent, we can stop as soon as we obtain the first solution satisfying (−1)kQk=N/∣N∣. Unlike all the preceding algorithms, here the qualifying k may occur before the period has been entered.
Example
Solve the equation x2−157y2=12.
Since 122<157, running the PQa algorithm with (P0,Q0,D)=(0,1,157) gives the following result (the red portion is the first period):
k
P
Q
a
A
B
G
G2−DB2
0
0
1
12
12
1
12
−13
1
12
13
1
13
1
13
12
2
1
12
1
25
2
25
−3
3
11
3
7
188
15
188
19
4
10
19
1
213
17
213
−4
5
9
4
5
1253
100
1253
9
6
11
9
2
2719
217
2719
−12
7
7
12
1
3972
317
3972
11
8
5
11
1
6691
534
6691
−11
9
6
11
1
10663
851
10663
12
10
5
12
1
17354
1385
17354
−9
11
7
9
2
45371
3621
45371
4
12
11
4
5
244209
19490
244209
−19
13
9
19
1
289580
23111
289580
3
14
10
3
7
2271269
181267
2271269
−12
15
11
12
1
2560849
204378
2560849
13
16
1
13
1
4832118
385645
4832118
−1
17
12
1
24
118531681
9459858
118531681
13
18
12
13
1
123363799
9845503
123363799
−12
19
1
12
1
241895480
19305361
241895480
3
20
11
3
7
1816632159
144983030
1816632159
−19
21
10
19
1
2058527639
164288391
2058527639
4
22
9
4
5
12109270354
966424985
12109270354
−9
23
11
9
2
26277068347
2097138361
26277068347
12
24
7
12
1
38386338701
3063563346
38386338701
−11
25
5
11
1
64663407048
5160701707
64663407048
11
26
6
11
1
103049745749
8224265053
103049745749
−12
27
5
12
1
167713152797
13384966760
167713152797
9
28
7
9
2
438476051343
34994198573
438476051343
−4
29
11
4
5
2360093409512
188355959625
2360093409512
19
30
9
19
1
2798569460855
223350158198
2798569460855
−3
31
10
3
7
21950079635497
1751807067011
21950079635497
12
32
11
12
1
24748649096352
1975157225209
24748649096352
−13
33
1
13
1
46698728731849
3726964292220
46698728731849
1
34
12
1
24
1145518138660728
91422300238489
1145518138660728
−13
35
12
13
1
1192216867392577
95149264530709
1192216867392577
12
The period length ℓ=17 is odd, so within two periods we need to consider Gk−12−157Bk−12 differs from 12 by a square factor, namely k=1,9,13,19,23,31; the corresponding solutions are the values of (fG,fB):
k
f
fGk−1
fBk−1
x
y
1
1
13
1
13
1
9
1
10663
851
10663
851
13
2
579160
46222
579160
46222
19
2
483790960
38610722
−579160
46222
23
1
26277068347
2097138361
−10663
851
31
1
21950079635497
1751807067011
−13
1
All (fG,fB) are the minimal positive integer solutions in all equivalence classes of x2−157y2=12. To obtain all solutions from these, we can use the fundamental solution of the corresponding Pell equation (46698728731849,3726964292220). For example, they can be transformed into the fundamental solutions (x,y), and the corresponding solutions are also listed in the table above.
Solve the equation x2−157y2=12.
This time, use the Lagrange–Matthews–Mollin algorithm to solve it. First, enumerate N=12 square factors:
f2=12 gives m=12, and the congruence P2≡157(mod12) has solutions z=±1,±5;
f2=22 gives m=3, and the congruence P2≡157(mod3) has solutions z=±1.
For every possible (f,z) combination, run the PQa algorithm with initial parameters (P0,Q0,D)=(z,∣m∣,D) and find the first occurrence of (−1)kQk=1; the corresponding (fGk−1,fBk−1) is a solution. The results are shown in the following table:
f
z
m
k
fGk−1
fBk−1
1
1
12
32
21950079635497
1751807067011
1
−1
12
2
13
1
1
5
12
24
26277068347
2097138361
1
−5
12
10
10663
851
2
1
3
20
483790960
38610722
2
−1
3
14
579160
46222
These are the minimal positive integer solutions in all equivalence classes listed above; the fundamental solution of the Pell equation can be used to transform them into fundamental solutions.
Solve the equation x2−79y2=±101.
Again, use the Lagrange–Matthews–Mollin algorithm to solve it. Since N=101 is prime, so necessarily f=1. Then, m=101, the corresponding congruence P2≡79(mod101) has solutions P=±33.
Running the PQa algorithm with (P0,Q0,D)=(33,101,79) gives the following result (the red portion is the first period):
k
P
Q
a
A
B
G
G2−DB2
0
33
101
0
0
1
−33
1010
1
−33
−10
2
1
2
35
909
2
13
9
2
2
5
37
−606
3
5
6
2
5
12
109
505
4
7
5
3
17
41
364
−303
5
8
3
5
90
217
1929
1010
6
7
10
1
107
258
2293
−707
7
3
7
1
197
475
4222
909
8
4
9
1
304
733
6515
−606
9
5
6
2
805
1941
17252
505
The period length ℓ=6 is even. There is no Qk=±1, so this case has no solution. Similarly, running the PQa algorithm with (P0,Q0,D)=(−33,101,79) also shows a similar situation. Therefore, this equation has no solution.
When D is a perfect square, direct factorization gives (x+yD)(x−yD)=N, so all solutions can be found by enumerating the factors of N. In particular, when N=1, the equation has only the solutions (±1,0); when N=−1 and D=1, the equation has no solution. ↩
Some Chinese sources also call it the negative Pell equation of the second type. ↩
That is, a rational number of the form n+21 with n∈Z. ↩
Note that the definition of a fundamental solution of a Pell equation is not the same as the definition of a fundamental unit in a real quadratic integer ring. First, in some real quadratic integer rings, a fundamental unit x+yD has half-integer x,y, so it is not a solution of a Pell equation. Second, a real quadratic integer ring has four fundamental units, but there is only one fundamental solution, because a fundamental solution requires x,y to both be positive. ↩
A relatively practical test and tool can be found here and in its references. The equation x2−Dy2=−1 has a solution for exactly the positive integers D listed in OEIS A031396. ↩