Pell Equation

Prerequisites: continued fractions, quadratic fields

Introduction

This article discusses solving the (generalized) Pell equation. A generalized Pell equation is an indeterminate equation in xx and yy of the form

x2−Dy2=N,x^2-Dy^2=N,

where DD is a positive integer that is not a perfect square1, and NN is a nonzero integer. The narrow sense of the Pell equation refers specifically to the cases N=1N=1 or N=±1N=\pm 1, and sometimes also includes N=±4N=\pm 4. Generalized Pell equations are closely related to finding quadratic integers of norm NN 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=1N=1. Correspondingly, the case N=−1N=-1 is called the negative Pell equation2 (negative Pell's equation).

Structure of the solutions

The integer solutions (x,y)(x,y) of a generalized Pell equation are closely connected with the quadratic integer x+yDx+y\sqrt{D}, so solutions of Pell equations are often written in the form x+yDx+y\sqrt{D} in the literature. The norm of a quadratic integer is

N(x+yD)=x2−Dy2,N(x+y\sqrt{D}) = x^2-Dy^2,

so a generalized Pell equation is roughly the problem of finding quadratic integers of norm NN. There is, however, a subtle difference. When xx and yy are both integers, x+yDx+y\sqrt{D} is certainly a quadratic integer; conversely, a quadratic integer does not necessarily require xx and yy to be integers—in the case D≡1(mod4)D\equiv 1\pmod 4, xx and yy 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\pm 1. For D≡2,3(mod4)D\equiv 2,3\pmod 4, finding such units only requires solving the generalized Pell equation for N=±1N=\pm 1; for D≡1(mod4)D\equiv 1\pmod 4, the cases N=±4N=\pm 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:

(x12−Dy12)(x22−Dy22)=(x1x2+Dy1y2)2−D(x1y2+x2y1)2.(x_1^2-Dy_1^2)(x_2^2-Dy_2^2)=(x_1x_2+Dy_1y_2)^2-D(x_1y_2+x_2y_1)^2.

This is equivalent to the multiplicativity of the norm of a quadratic integer:

N(x1+y1D)N(x2+y2D)=N((x1+y1D)(x2+y2D))=N((x1x2+Dy1y2)+(x1y2+x2y1)D).\begin{aligned} N\left(x_1+y_1\sqrt{D}\right)N\left(x_2+y_2\sqrt{D}\right) &= N\left((x_1+y_1\sqrt{D})(x_2+y_2\sqrt{D})\right) \\ &= N\left((x_1x_2+Dy_1y_2)+(x_1y_2+x_2y_1)\sqrt{D}\right). \end{aligned}

Using this identity, integer solutions of x2−Dy2=N1x^2-Dy^2=N_1 and x2−Dy2=N2x^2-Dy^2=N_2 can be combined to obtain an integer solution of x2−Dy2=N1N2x^2-Dy^2=N_1N_2. 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=NN_1=N and N2=1N_2=1 shows that if one solution of x2−Dy2=Nx^2-Dy^2=N and all solutions of the corresponding Pell equation x2−Dy2=1x^2-Dy^2=1 are known, more solutions of x2−Dy2=Nx^2-Dy^2=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=1x^2-Dy^2=1 is a hyperbola whose real axis is the xx-axis and imaginary axis is the yy-axis. Every point on the hyperbola corresponds uniquely to a nonzero value of x+yDx+y\sqrt{D}: the left branch corresponds to negative values and the right branch to positive values. Moreover, on each branch, the values of x+yDx+y\sqrt{D} increase strictly from bottom to top. The values of quadratic integers therefore give the solutions of the Pell equation a natural order in x+yDx+y\sqrt{D}.

The hyperbola is symmetric about both the xx-axis and the yy-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>1x+y\sqrt{D}>1. If the equation has a nontrivial solution besides (±1,0)(\pm 1,0), then there is a solution (x1,y1)(x_1,y_1) in the first quadrant for which x+yDx+y\sqrt{D} 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)(x_k,y_k) satisfying xk+ykD=(x1+y1D)kx_k+y_k\sqrt{D}=(x_1+y_1\sqrt{D})^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)(x_1,y_1) be the fundamental solution of x2−Dy2=1x^2-Dy^2=1. Then all its solutions are

{(x,y):x+yD=±(x1+y1D)k,k∈Z}.\{(x,y):x+y\sqrt{D}=\pm(x_1+y_1\sqrt{D})^k,k\in\mathbf Z\}.
Proof

First prove that there are no other solutions in the first quadrant. Suppose there is another solution x+yDx+y\sqrt{D} and, for some k≥0k\ge 0,

xk+ykD<x+yD<xk+1+yk+1D.x_k+y_k\sqrt{D}< x+y\sqrt{D}< x_{k+1}+y_{k+1}\sqrt{D}.

Geometrically, this means that the lattice point (x,y)(x,y) lies strictly between (xk,yk)(x_k,y_k) and (xk+1,yk+1)(x_{k+1},y_{k+1}) on the hyperbola. Multiplying the inequality by xk−ykD=(xk+ykD)−1x_k-y_k\sqrt{D}=(x_k+y_k\sqrt{D})^{-1} gives

1<(x+yD)(xk−ykD)=(xxk−Dyyk)+(xky−xyk)D<x1+y1D.1< (x+y\sqrt{D})(x_k-y_k\sqrt{D})=(xx_k-Dyy_k)+(x_ky-xy_k)\sqrt{D} < x_1+y_1\sqrt{D}.

By the monotonicity mentioned above, this says that (xxk−Dyyk,xky−xyk)(xx_k-Dyy_k,x_ky-xy_k) is an integer point between (1,0)(1,0) and (x1,y1)(x_1,y_1), contradicting the choice of (x1,y1)(x_1,y_1).

When extending the first-quadrant solutions to the whole plane, replacing kk by its opposite (taking the entire expression's inverse) gives reflection about the xx-axis, while negating the entire expression gives reflection about the origin. Adding the trivial solution for k=0k=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=1x^2-Dy^2=1 always has an integer solution other than (±1,0)(\pm 1,0).

Proof

First, Dirichlet's theorem states that there are infinitely many pairs of positive integers (x,y)(x,y) satisfying

∣xy−D∣≤1y2\left|\dfrac{x}{y}-\sqrt{D}\right| \le \dfrac{1}{y^2}

and all of them satisfy

∣x2−Dy2∣=y2∣xy−D∣∣xy+D∣≤1y2+2D<1+2D.|x^2-Dy^2|=y^2\left|\dfrac{x}{y}-\sqrt{D}\right|\left|\dfrac{x}{y}+\sqrt{D}\right| \le \dfrac{1}{y^2}+2\sqrt{D}<1+2\sqrt{D}.

Hence there is an integer m∈(−1−2D,1+2D)m\in(-1-2\sqrt{D},1+2\sqrt{D}) such that infinitely many pairs (x,y)(x,y) satisfy x2−Dy2=mx^2-Dy^2 = m. Classify these (x,y)(x,y) by their residues modulo mm. For some pair of integers (x0,y0)(x_0,y_0), infinitely many (x,y)(x,y) must satisfy x≡x0(modm)x\equiv x_0\pmod m and y≡y0(modm)y\equiv y_0\pmod m. Choose two distinct pairs (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) satisfying these conditions. Then

x1+y1Dx2+y2D=x1x2−Dy1y2m+x2y1−x1y2mD.\dfrac{x_1+y_1\sqrt{D}}{x_2+y_2\sqrt{D}}=\dfrac{x_1x_2-Dy_1y_2}{m}+\dfrac{x_2y_1-x_1y_2}{m}\sqrt{D}.

By the congruences,

x1x2−Dy1y2≡x02−Dy02=m≡0(mod∣m∣),x2y1−x1y2≡x0y0−x0y0=0(mod∣m∣),\begin{aligned} x_1x_2-Dy_1y_2 &\equiv x_0^2-Dy_0^2 = m \equiv 0 \pmod{|m|},\\ x_2y_1-x_1y_2 &\equiv x_0y_0-x_0y_0 = 0 \pmod{|m|}, \end{aligned}

so the right-hand side is an integer quadratic integer. Since (x1,y1)≠(x2,y2)(x_1,y_1)\neq(x_2,y_2), 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)D\equiv 1\pmod 4 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=Nx^2-Dy^2=N is also a hyperbola in the plane, symmetric about both the xx-axis and the yy-axis. As noted above, some solutions of the equation x2−Dy2=Nx^2-Dy^2=N may differ only by a factor that is a Pell equation solution, so the solutions of x2−Dy2=Nx^2-Dy^2=N can be divided into equivalence classes. For two solutions (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) of x2−Dy2=Nx^2-Dy^2=N, if there is a Pell equation solution (u,v)(u,v) such that x2+y2D=(x1+y1D)(u+vD)x_2+y_2\sqrt{D}=(x_1+y_1\sqrt{D})(u+v\sqrt{D}), then the solutions (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) are called equivalent. The necessary and sufficient conditions for two solutions to be equivalent are

N∣(x1x2−Dy1y2), N∣(x2y1−x1y2).N\mid (x_1x_2-Dy_1y_2),\ N\mid (x_2y_1-x_1y_2).

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 yy: if it is unique, it is called the fundamental solution of that class; otherwise, the class has two solutions with nonnegative minimal yy, symmetric about the yy-axis, and the one with x>0x>0 is chosen as the fundamental solution. Thus solving x2−Dy2=Nx^2-Dy^2=N amounts to finding its set UU of fundamental solutions. Let (r,s)(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}.\{(x,y):x+y\sqrt{D}=\pm(r+s\sqrt{D})^k(u+v\sqrt{D}),k\in\mathbf Z,u+v\sqrt{D}\in U\}.

There are only finitely many fundamental solutions of a generalized Pell equation. Indeed, the general solution above shows that ∣u+vD∣|u+v\sqrt{D}| must lie between r−sDr-s\sqrt{D} and r+sDr+s\sqrt{D}. 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)(u,v) and the fundamental Pell solution (r,s)(r,s) by the recurrence

xk=2rxk−1−xk−2, yk=2ryk−1−yk−2,x_{k} = 2rx_{k-1} - x_{k-2},\ y_{k} = 2ry_{k-1} - y_{k-2},

where xk+ykD=(r+sD)k(u+vD)x_k+y_k\sqrt{D}=(r+s\sqrt{D})^k(u+v\sqrt{D}). This is because xnx_n and yny_n can each be written, for some real pair (A,B)(A,B), in the form A(r+sD)k+B(r−sD)kA(r+s\sqrt{D})^k+B(r-s\sqrt{D})^k. By Vieta's theorem, r±sDr\pm s\sqrt{D} are the two real roots of x2−2rx+1=0x^2-2rx+1=0, so xnx_n and yny_n 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,DP_0,Q_0,D satisfy Q0≠0Q_0\neq 0, D>0D>0 and not a perfect square, and P02≡D(modQ0)P_0^2\equiv D\pmod{Q_0}. Then the continued-fraction expansion of the quadratic irrational

ω=P0+DQ0\omega=\dfrac{P_0+\sqrt{D}}{Q_0}

can be found by the following recurrence, with notation [a0,a1,⋯ ][a_0,a_1,\cdots]:

ak=⌊Pk+DQk⌋, Pk+1=akQk−Pk, Qk+1=D−Pk+12Qk.a_k = \left\lfloor\dfrac{P_k+\sqrt{D}}{Q_k}\right\rfloor,\ P_{k+1} = a_kQ_k - P_k,\ Q_{k+1} = \dfrac{D-P_{k+1}^2}{Q_k}.

The numerator and denominator AkA_k and BkB_k of the kk-th convergent of ω\omega are then given by the following recurrence:

Ak=akAk−1+Ak−2, Bk=akBk−1+Bk−2A_k = a_kA_{k-1} + A_{k-2},\ B_k = a_kB_{k-1} + B_{k-2}

with A−1=1A_{-1} = 1, A−2=0A_{-2}=0, B−1=0B_{-1}=0, and B−2=1B_{-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)(P_k,Q_k,a_k) eventually enters a cycle, so the algorithm always terminates in finitely many steps. Let the minimum period length be ℓ\ell, and let the earliest starting position of the cycle be k0k_0. Then the continued-fraction expansion is

ω=[a0,⋯ ,ak0−1,ak0,⋯ ,ak0+ℓ−1‾].\omega=[a_0,\cdots,a_{k_0-1},\overline{a_{k_0},\cdots,a_{k_0+\ell-1}}].

To solve Pell equations with the PQa algorithm, we need the following result:

Theorem

Continue with the notation above. Let Gk=Q0Ak−P0BkG_k=Q_0A_k-P_0B_k. Then the integer pair (Gk−1,Bk−1)(G_{k-1},B_{k-1}) satisfies

Gk−12−DBk−12=(−1)kQ0Qk,G_{k-1}^2-DB_{k-1}^2=(-1)^{k}Q_0Q_{k},

and their greatest common divisor gcd⁡(Gk−1,Bk−1)\gcd(G_{k-1},B_{k-1}) divides QkQ_{k}.

Proof

Let the kk-th remainder (complete quotient) in the continued-fraction expansion of ω\omega be ωk\omega_{k}, so

ω=[a0,a1,⋯ ,ak−1,ωk]=ωkAk−1+Ak−2ωkBk−1+Bk−2.\omega = [a_0,a_1,\cdots,a_{k-1},\omega_k] = \dfrac{\omega_k A_{k-1}+A_{k-2}}{\omega_k B_{k-1}+B_{k-2}}.

Substitute ω=(P0+D)/Q0\omega=(P_0+\sqrt{D})/Q_0 and ωk=(Pk+D)/Qk\omega_k=(P_k+\sqrt{D})/Q_k into the equation above to obtain

P0+DQ0=(Pk+D)Ak−1+QkAk−2(Pk+D)Bk−1+QkBk−2.\dfrac{P_0+\sqrt{D}}{Q_0} = \dfrac{(P_k+\sqrt{D})A_{k-1}+Q_kA_{k-2}}{(P_k+\sqrt{D})B_{k-1}+Q_kB_{k-2}}.

Cancel the denominators on both sides, compare the coefficients of the rational and irrational parts, and substitute the expression for GkG_k. This gives

Gk−1=PkBk−1+QkBk−2,DBk−1=PkGk−1+QkGk−2.\begin{aligned} G_{k-1} &= P_kB_{k-1} + Q_kB_{k-2},\\ DB_{k-1} &= P_kG_{k-1} + Q_kG_{k-2}. \end{aligned}

Multiply the first equation by Gk−1G_{k-1} and subtract the second multiplied by Bk−1B_{k-1}:

Gk−12−DBk−12=(Bk−2Gk−1−Bk−1Gk−2)Qk=(Ak−1Bk−2−Bk−1Ak−2)Q0Qk=(−1)kQ0Qk.\begin{aligned} G_{k-1}^2-DB_{k-1}^2 &= (B_{k-2}G_{k-1}-B_{k-1}G_{k-2})Q_k \\ &= (A_{k-1}B_{k-2}-B_{k-1}A_{k-2})Q_0Q_k \\ &= (-1)^kQ_0Q_k. \end{aligned}

The last step uses the difference formula for convergents. This proves the first conclusion.

To prove the second conclusion, substitute the expression for GkG_k into the first conclusion:

(Q0Ak−1−P0Bk−1)2−DBk−12=(−1)kQ0Qk.(Q_0A_{k-1}-P_0B_{k-1})^2 - DB_{k-1}^2 = (-1)^kQ_0Q_k.

Since Q0∣(P02−D)Q_0\mid(P_0^2-D), we have

Q0Ak−12+(P02−DQ0Bk−1−2P0Ak−1)Bk−1=(−1)kQk.Q_0A_{k-1}^2 +\left(\dfrac{P_0^2-D}{Q_0}B_{k-1}- 2P_0A_{k-1}\right)B_{k-1} = (-1)^kQ_k.

Therefore, gcd⁡(Gk−1,Bk−1)=gcd⁡(Q0Ak−1,Bk−1)\gcd(G_{k-1},B_{k-1}) = \gcd(Q_0A_{k-1},B_{k-1}) divides QkQ_k.

This result gives a way to find solutions of x2−Dy2=Nx^2-Dy^2=N. Choose Q0>0Q_0>0 appropriately and choose P0P_0 as a solution of the congruence P02≡D(modQ0)P_0^2\equiv D\pmod{Q_0}. Run the PQa algorithm on (P0+D)/Q0(P_0+\sqrt{D})/Q_0 until (−1)kQ0Qk=N(-1)^kQ_0Q_{k}=N is found. Then (Gk−1,Bk−1)(G_{k-1},B_{k-1}) is a solution of the original equation. Moreover, if Qk=±1Q_k=\pm 1, this solution is primitive, meaning that Gk−1G_{k-1} and Bk−1B_{k-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=1x^2-Dy^2=1, run the PQa algorithm from (P0,Q0,D)=(0,1,D)(P_0,Q_0,D)=(0,1,D) until (−1)kQk=1(-1)^kQ_k=1. Then (Ak−1,Bk−1)(A_{k-1},B_{k-1}) is a Pell equation solution, because at this point Gk−1G_{k-1} is exactly Ak−1A_{k-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\sqrt{D}. We have the following result:

D=[⌊D⌋,a1,⋯ ,aℓ−1,2⌊D⌋‾].\sqrt{D} = [\lfloor\sqrt{D}\rfloor,\overline{a_1,\cdots,a_{\ell-1},2\lfloor\sqrt{D}\rfloor}].

The period length is ℓ\ell, and it starts at term 11 (with indices starting at 00). The remainder at term ℓ\ell is ⌊D⌋+D\lfloor\sqrt{D}\rfloor+\sqrt{D}, which shows that Qℓ=1Q_{\ell}=1. Therefore, if ℓ\ell is even, (Aℓ−1,Bℓ−1)(A_{\ell-1},B_{\ell-1}) is a nontrivial Pell equation solution; if ℓ\ell is odd, (A2ℓ−1,B2ℓ−1)(A_{2\ell-1},B_{2\ell-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/yx/y corresponding to all positive integer Pell solutions (x,y)(x,y) occur among the convergents of D\sqrt{D}, so (x,y)(x,y) must be some (Ak,Bk)(A_k,B_k) during the PQa process. Second, Qk=1Q_k=1 does not occur at any other position besides the end of a period, because the recurrences for AkA_k and BkB_k 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=Nx^2-Dy^2=N has a positive integer solution (x,y)(x,y). If ∣N∣<D|N|<\sqrt{D}, then xy\dfrac{x}{y} is a convergent of D\sqrt{D}.

Proof

When N>0N>0, x2−Dy2>0x^2-Dy^2>0, so x>yDx>y\sqrt{D}. Hence

∣xy−D∣=Ny(x+yD)<N2y2D<12y2.\left|\dfrac{x}{y}-\sqrt{D}\right| = \dfrac{N}{y(x+y\sqrt{D})}<\dfrac{N}{2y^2\sqrt{D}}<\dfrac{1}{2y^2}.

By Legendre's criterion, xy\dfrac{x}{y} is a convergent of D\sqrt{D}.

When N<0N<0, x>yDx>y\sqrt{D} no longer holds. Instead, consider a solution of y2−1Dx2=−NDy^2-\dfrac{1}{D}x^2=-\dfrac{N}{D}. Since ∣N∣D<1D\dfrac{|N|}{D}<\sqrt{\dfrac{1}{D}}, the preceding argument still applies. Thus yx\dfrac{y}{x} is a convergent of 1D\dfrac{1}{\sqrt{D}}. By the reciprocal theorem, xy\dfrac{x}{y} is also a convergent of D\sqrt{D}.

Theorem

During the PQa algorithm above starting from (P0,Q0,D)=(0,1,D)(P_0,Q_0,D)=(0,1,D), Qk=1Q_k=1 implies ℓ∣k\ell\mid k.

Proof

In the continued-fraction expansion of D\sqrt{D}, every remainder other than the 00th is a purely periodic continued fraction. Let Qk=1Q_k=1. By Galois's result, the remainder must satisfy ωk=Pk+D>1\omega_k=P_k+\sqrt{D}>1 and its conjugate −1<Pk−D<0-1<P_k-\sqrt{D}<0, which means Pk=⌊D⌋P_k=\lfloor\sqrt{D}\rfloor. Therefore ωk\omega_k equals ωℓ\omega_\ell. Repetition of a remainder means that the continued fraction has entered its cycle. If kk were not a multiple of ℓ\ell, this would contradict the minimality of ℓ\ell. Hence ℓ∣k\ell\mid k.

Combining the discussion in this section, expanding D\sqrt{D} as a continued fraction—that is, running the PQa algorithm from (P0,Q0,D)=(0,1,D)(P_0,Q_0,D)=(0,1,D)—reaches the end of the first period when Qℓ=1Q_\ell=1 is first obtained. If ℓ\ell is even, (Aℓ−1,Bℓ−1)(A_{\ell-1},B_{\ell-1}) is the fundamental solution; otherwise (A2ℓ−1,B2ℓ−1)(A_{2\ell-1},B_{2\ell-1}) is the fundamental solution. When ℓ\ell 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)2A_{2\ell-1}+B_{2\ell-1}\sqrt{D}=(A_{\ell-1}+B_{\ell-1}\sqrt{D})^2, so the fundamental solution can be computed directly from (Aℓ−1,Bℓ−1)(A_{\ell-1},B_{\ell-1}). All other Pell equation solutions can be computed from the fundamental solution.

Example
  1. Solve x2−14y2=1x^2-14y^2=1.

    Running the PQa algorithm from (P0,Q0,D)=(0,1,14)(P_0,Q_0,D)=(0,1,14) gives the following results (the red entries form the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    00001133331133−5-5
    1133551\color{red}{1}44114422
    2222222\color{red}{2}1111331111−5-5
    3322551\color{red}{1}151544151511
    4433116\color{red}{6}1011012727101101−5-5
    55335511116116313111611622

    The period length ℓ=4\ell=4 is even. The smallest positive integer solution is (G3,B3)=(15,4)(G_3,B_3)=(15,4).

  2. Solve x2−41y2=1x^2-41y^2=1.

    Running the PQa algorithm from (P0,Q0,D)=(0,1,41)(P_0,Q_0,D)=(0,1,41) gives the following results (the red entries form the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    00001166661166−5-5
    1166552\color{red}{2}131322131355
    2244552\color{red}{2}3232553232−1-1
    33661112\color{red}{12}397397626239739755
    44665522826826129129826826−5-5
    55445522204920493203202049204911
    66661112122541425414396939692541425414−5-5
    77665522528775287782588258528775287755

    The period length ℓ=3\ell=3 is odd. The smallest positive integer solution is (G5,B5)=(2049,320)(G_5,B_5)=(2049,320). It can also be computed from (G2,B2)=(32,5)(G_2,B_2)=(32,5):

    (32+541)2=2049+32041.(32+5\sqrt{41})^2=2049+320\sqrt{41}.

Negative Pell equation

By the discussion in the preceding section, solutions of the negative Pell equation also correspond to convergents of D\sqrt{D}, and can occur only when (−1)kQk=−1(-1)^kQ_k=-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 ℓ\ell is odd. When a solution exists, (Aℓ−1,Bℓ−1)(A_{\ell-1},B_{\ell-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=−1x^2-Dy^2=-1 has a solution with fundamental solution (x1,y1)(x_1,y_1). Then all integer solutions of x2−Dy2=±1x^2-Dy^2=\pm 1 belong to

{(x,y):x+yD=±(x1+y1D)k,k∈Z}.\{(x,y):x+y\sqrt{D}=\pm(x_1+y_1\sqrt{D})^k,k\in\mathbf Z\}.

In particular, the integer solution (x2,y2)(x_2,y_2) satisfying x2+y2D=(x1+y1D)2x_2+y_2\sqrt{D}=(x_1+y_1\sqrt{D})^2 is the fundamental solution of x2−Dy2=1x^2-Dy^2=1.

Proof

By symmetry, it is enough to consider positive integer solutions (x,y)(x,y), that is, x+yD>1x+y\sqrt{D}>1. However, since x2−Dy2=±1x^2-Dy^2=\pm 1 consists of two hyperbolas, x+yDx+y\sqrt{D} does not correspond one-to-one with (x,y)(x,y). To handle this difficulty, first prove that the above (x2,y2)(x_2,y_2) is the fundamental solution of x2−Dy2=1x^2-Dy^2=1.

Clearly, (x2,y2)(x_2,y_2) solves x2−Dy2=1x^2-Dy^2=1. If (z,w)(z,w) is the fundamental solution of x2−Dy2=1x^2-Dy^2=1, then 1<z+wD≤x2+y2D1<z+w\sqrt{D}\le x_2+y_2\sqrt{D}. If the right inequality is strict, divide the inequality by x1+y1Dx_1+y_1\sqrt{D} to obtain −x1+y1D<(z+wD)(−x1+y1D)<x1+y1D-x_1+y_1\sqrt{D}<(z+w\sqrt{D})(-x_1+y_1\sqrt{D})<x_1+y_1\sqrt{D}. Expanding the middle expression gives x′+y′Dx'+y'\sqrt{D}, whose norm is −1-1, and (x′,y′)(x',y') is also an integer solution. Taking the reciprocal of this inequality shows that −x′+y′D-x'+y'\sqrt{D} also lies between −x1+y1D-x_1+y_1\sqrt{D} and x1+y1Dx_1+y_1\sqrt{D}. The quadratic integers ±x′+y′D\pm x'+y'\sqrt{D} are reciprocals of each other, so one of them must be greater than 11. But no other quadratic integer of norm −1-1 should occur between 11 and x1+y1Dx_1+y_1\sqrt{D}, contradicting the minimality of x1+y1Dx_1+y_1\sqrt{D}. Thus x2+y2D=z+wDx_2+y_2\sqrt{D}=z+w\sqrt{D}, and (x2,y2)(x_2,y_2) is the fundamental solution of x2−Dy2=1x^2-Dy^2=1.

Based on this, if a solution of x2−Dy2=±1x^2-Dy^2=\pm 1 were not of the form (x1+y1D)k(x_1+y_1\sqrt{D})^k, there would be a kk such that (x1+y1D)2k<x+yD<(x1+y1D)2k+2(x_1+y_1\sqrt{D})^{2k}<x+y\sqrt{D}<(x_1+y_1\sqrt{D})^{2k+2}. Canceling the factor (x1+y1)2k+1(x_1+y_1)^{2k+1} would produce a quadratic integer x′+y′D≠1x'+y'\sqrt{D}\neq 1 of norm ±1\pm 1 between −x1+y1D-x_1+y_1\sqrt{D} and x1+y1Dx_1+y_1\sqrt{D}. Repeating the reciprocal argument from the preceding paragraph gives a contradiction to the minimality of x1+y1Dx_1+y_1\sqrt{D}. The proposition follows.

Since (Aℓ−1,Bℓ−1)(A_{\ell-1},B_{\ell-1}) is the smallest positive integer solution of the negative Pell equation, all positive integer solutions of x2−Dy2=±1x^2-Dy^2=\pm 1 lie in

{(x,y):x+yD=(Aℓ−1+Bℓ−1D)k,k∈N+}\{(x,y):x+y\sqrt{D}=(A_{\ell-1}+B_{\ell-1}\sqrt{D})^k,k\in\mathbf N_+\}

and these positive integer solutions necessarily correspond to the convergents of D\sqrt{D} immediately before the end of a period. Since the numerators and denominators of convergents increase strictly, for every k∈N+k\in\mathbf N_+ we have

(Aℓ−1+Bℓ−1D)k=Akℓ−1+Bkℓ−1D.(A_{\ell-1}+B_{\ell-1}\sqrt{D})^k = A_{k\ell-1}+B_{k\ell-1}\sqrt{D}.

Among these positive integer solutions, odd kk gives negative Pell solutions and even kk 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\sqrt{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=−1x^2-Dy^2=-1 has a solution, then 44 does not divide DD and DD has no prime factor of the form 4k+34k+3. Conversely, if D=2D=2 or DD is a prime of the form 4k+14k+1, then the equation necessarily has a solution.

Proof

First, a solution of the negative Pell equation means that −1-1 is a quadratic residue modulo DD, and therefore −1-1 is also a quadratic residue modulo every factor dd of DD. Thus d≠4d\neq 4 and dd is not a prime of the form 4k+34k+3. Conversely, x2−2y2=−1x^2-2y^2=-1 has the nontrivial solution (1,1)(1,1). It remains to handle the case where DD is a prime of the form 4k+14k+1.

Let DD be a prime of the form 4k+14k+1. We prove that x2−Dy2=−1x^2-Dy^2=-1 has a solution by starting with the fundamental solution pair (u,v)(u,v) of x2−Dy2=1x^2-Dy^2=1 and constructing a solution (α,β)(\alpha,\beta) of x2−Dy2=−1x^2-Dy^2=-1. If uu were even, reducing u2−Dv2=1u^2-Dv^2=1 modulo 44 would give v2≡−1(mod4)v^2\equiv -1\pmod 4, but −1-1 is not a quadratic residue modulo 44. Hence uu is odd. Consider Dv2=u2−1=(u+1)(u−1)Dv^2=u^2-1=(u+1)(u-1). Since uu is odd, gcd⁡(u+1,u−1)=gcd⁡(u+1,2)=2\gcd(u+1,u-1)=\gcd(u+1,2)=2. Therefore, distributing the factors of Dv2Dv^2 between u+1u+1 and u−1u-1, one must be 2α22\alpha^2 and the other 2Dβ22D\beta^2, where α\alpha and β\beta are coprime positive integers and v=2αβv=2\alpha\beta. Substituting u=α2+Dβ2u=\alpha^2+D\beta^2 and v=2αβv=2\alpha\beta into u2−Dv2=1u^2-Dv^2=1 gives α2−Dβ2=±1\alpha^2-D\beta^2=\pm 1. Since (u,v)(u,v) is the fundamental Pell solution and (α,β)(\alpha,\beta) is a smaller positive integer pair than (u,v)(u,v), the right-hand side cannot be +1+1, so it must be −1-1. Thus x2−Dy2=−1x^2-Dy^2=-1 has the solution (α,β)(\alpha,\beta).

If DD is composite, having no prime factor of the form 4k+34k+3 and having no square factor still does not guarantee a solution to x2−Dy2=−1x^2-Dy^2=-1; for example, x2−34y2=−1x^2-34y^2=-1 has no solution.

Example

From the calculations in the preceding examples, x2−14y2=−1x^2-14y^2=-1 has no solution, while the smallest positive integer solution of x2−41y2=−1x^2-41y^2=-1 is (G2,B2)=(32,5)(G_2,B_2)=(32,5).

The case of norm ±4

We now discuss solutions of x2−Dy2=±4x^2-Dy^2=\pm 4. Their behavior depends on D mod 4D\bmod 4.

Some cases are easy. If D≡0(mod4)D\equiv 0\pmod 4, then xx is even, so (x/2,y)(x/2,y) solves u2−(D/4)v2=±1u^2-(D/4)v^2=\pm 1. In the remaining cases, x,yx,y must be both odd or both even. If both are odd, reducing both sides modulo 44 gives D≡1(mod4)D\equiv 1\pmod 4. Thus, if D≡2,3(mod4)D\equiv 2,3\pmod 4, x,yx,y must both be even, and (x/2,y/2)(x/2,y/2) solves u2−Dv2=±1u^2-Dv^2=\pm 1. Therefore, except when D≡1(mod4)D\equiv 1\pmod 4, solutions of x2−Dy2=±4x^2-Dy^2=\pm 4 can be obtained from the corresponding (negative) Pell equation solutions for x,yx,y.

Now consider D≡1(mod4)D\equiv 1\pmod 4, 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)(P_0,Q_0,D)=(1,2,D). When Qℓ=2Q_\ell=2 is first obtained, the end of the first period has been reached. If the period length ll is even, (Gℓ−1,Bℓ−1)(G_{\ell-1},B_{\ell-1}) is the fundamental solution of x2−Dy2=4x^2-Dy^2=4; otherwise the pair (Gℓ−1,Bℓ−1)(G_{\ell-1},B_{\ell-1}) is the fundamental solution of x2−Dy2=−4x^2-Dy^2=-4. Starting from the pair (Gℓ−1,Bℓ−1)(G_{\ell-1},B_{\ell-1}), all solutions of x2−Dy2=±4x^2-Dy^2=\pm 4 are given by the following expression:

{(x,y):x+yD2=±(Gℓ−1+Bℓ−1D2)k,k∈Z}.\left\{(x,y):\dfrac{x+y\sqrt{D}}{2}=\pm\left(\dfrac{G_{\ell-1}+B_{\ell-1}\sqrt{D}}{2}\right)^k,k\in\mathbf Z\right\}.

If the period length ℓ\ell is even, all of these solve x2−Dy2=4x^2-Dy^2=4; otherwise, when kk is odd, (x,y)(x,y) solves x2−Dy2=−4x^2-Dy^2=-4, and when kk is even, (x,y)(x,y) solves x2−Dy2=4x^2-Dy^2=4.

The correctness of this algorithm depends on the following fact:

Theorem

Suppose x2−Dy2=±4x^2-Dy^2=\pm 4 has a positive integer solution (x,y)(x,y). If D≡1(mod4)D\equiv 1\pmod 4, then (x+y)/2y\dfrac{(x+y)/2}{y} is a convergent of 1+D2\dfrac{1+\sqrt{D}}{2}.

Proof

First note that x,yx,y must have the same parity, so (x+y)/2(x+y)/2 is an integer. If (x,y)(x,y) solves x2−Dy2=4x^2-Dy^2=4, then x>yD>2yx>y\sqrt{D}>2y, and hence

∣(x+y)/2y−1+D2∣=2y(x+yD)<12y2.\left|\dfrac{(x+y)/2}{y}-\dfrac{1+\sqrt{D}}{2}\right| = \dfrac{2}{y(x+y\sqrt{D})}<\dfrac{1}{2y^2}.

By Legendre's criterion, (x+y)/2y\dfrac{(x+y)/2}{y} is a convergent of 1+D2\dfrac{1+\sqrt{D}}{2}.

If (x,y)(x,y) solves x2−Dy2=−4x^2-Dy^2=-4, it is enough to prove 4y<x+yD4y<x+y\sqrt{D} to establish the same inequality. This holds for all cases except D=5,13D=5,13. For D=5,13D=5,13, substituting x=Dy2−4x=\sqrt{Dy^2-4} shows that the inequality is equivalent to 2(D−2)y2>12(\sqrt{D}-2)y^2>1. Apart from (D,y)=(5,1)(D,y)=(5,1), this holds for all D=5,13D=5,13 and positive integers yy. In the remaining case (D,y)=(5,1)(D,y)=(5,1), the solution of x2−5y2=−4x^2-5y^2=-4 is (x,y)=(1,1)(x,y)=(1,1), and we only need to verify that 11\dfrac{1}{1} is a convergent of 1+52=[1‾]\dfrac{1+\sqrt{5}}{2}=[\overline{1}], which is immediate.

Theorem

Let DD be a positive nonsquare integer. The continued-fraction expansion of the quadratic irrational ω=1+D2\omega=\dfrac{1+\sqrt{D}}{2} has the form

ω=[⌊ω⌋,a1,⋯ ,aℓ−1,2⌊ω⌋−1‾],\omega = [\lfloor\omega\rfloor,\overline{a_1,\cdots,a_{\ell-1},2\lfloor\omega\rfloor-1}],

where ℓ\ell is the period length and ak=aℓ−ka_k=a_{\ell-k} for every 1<k<ℓ1<k<\ell.

Proof

Since ⌊ω⌋−1+ω>1\lfloor\omega\rfloor-1+\omega>1 and its conjugate is ⌊ω⌋−ω\lfloor\omega\rfloor - \omega, which lies between −1-1 and 00, Galois's result shows that ⌊ω⌋−1+ω\lfloor\omega\rfloor-1+\omega is a purely periodic continued fraction:

⌊ω⌋−1+ω=[2⌊ω⌋−1,a1,⋯ ,aℓ−1‾].\lfloor\omega\rfloor-1+\omega = [\overline{2\lfloor\omega\rfloor-1,a_1,\cdots,a_{\ell-1}}].

Galois's result about the reciprocal of the negative conjugate gives

1ω−⌊ω⌋=[aℓ−1,⋯ ,a1,2⌊ω⌋−1‾].\dfrac{1}{\omega-\lfloor\omega\rfloor} = [\overline{a_{\ell-1},\cdots,a_1,2\lfloor\omega\rfloor-1}].

Therefore, by the definition of continued fractions,

⌊ω⌋−1+ω=2⌊ω⌋−1+11ω−⌊ω⌋=[2⌊ω⌋−1,aℓ−1,⋯ ,a1,2⌊ω⌋−1‾].\lfloor\omega\rfloor-1+\omega = 2\lfloor\omega\rfloor-1 + \dfrac{1}{\dfrac{1}{\omega-\lfloor\omega\rfloor}} = [2\lfloor\omega\rfloor-1,\overline{a_{\ell-1},\cdots,a_1,2\lfloor\omega\rfloor-1}].

The uniqueness of continued-fraction expansions gives ak=aℓ−ka_k=a_{\ell-k} for all 1<k<ℓ1<k<\ell, proving the claimed expansion.

Theorem

Let D≡1(mod4)D\equiv 1\pmod 4. During the PQa algorithm above starting from (P0,Q0,D)=(1,2,D)(P_0,Q_0,D)=(1,2,D), Qk=2Q_k=2 implies ℓ∣k\ell\mid k.

Proof

In the continued-fraction expansion of 1+D2\dfrac{1+\sqrt{D}}{2}, every remainder other than the 00th is a purely periodic continued fraction. Let Qk=2Q_k=2. By Galois's result, the conjugate of ωk=Pk+D2\omega_k=\dfrac{P_k+\sqrt{D}}{2} satisfies −1<Pk−D2<0-1<\dfrac{P_k-\sqrt{D}}{2}<0, or equivalently D−2<Pk<D\sqrt{D}-2<P_k<\sqrt{D}. Since Qk∣Pk2−DQ_k\mid P_k^2-D always holds in the PQa algorithm (see the proof of algorithm correctness), PkP_k must be odd. Thus PkP_k is uniquely determined, namely Pk=P0+2(⌊ω⌋−1)P_k=P_0+2(\lfloor\omega\rfloor-1), so ωk=ωℓ\omega_k=\omega\ell. Repetition of a remainder means that the continued fraction has entered its cycle. If kk were not a multiple of ℓ\ell, this would contradict the minimality of ℓ\ell. Hence ℓ∣k\ell\mid k.

Theorem

Let (x1,y1)(x_1,y_1) be the smallest positive integer solution of x2−Dy2=±4x^2-Dy^2=\pm 4. Then all its solutions are

{(x,y):x+yD2=±(x1+y1D2)k,k∈Z}.\left\{(x,y):\dfrac{x+y\sqrt{D}}{2}=\pm\left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^k,k\in\mathbf Z\right\}.
Proof

By symmetry, it is enough to consider positive integer solutions (x,y)(x,y). The only thing to prove is that every pair (x,y)(x,y) in the set is indeed an integer solution of x2−Dy2=±4x^2-Dy^2=\pm 4. The rest repeats the proof of the solution structure for x2−Dy2=±1x^2-Dy^2=\pm 1.

Specifically, for any two integer solutions (x1,y1)(x_1,y_1) and (x2,y2)(x_2,y_2) of x2−Dy2=±4x^2-Dy^2=\pm 4, define a positive real pair (x3,y3)(x_3,y_3) by

x3+y3D2=x1+y1D2x2+y2D2.\dfrac{x_3+y_3\sqrt{D}}{2} = \dfrac{x_1+y_1\sqrt{D}}{2}\dfrac{x_2+y_2\sqrt{D}}{2}.

Expanding the right side and comparing the rational and irrational coefficients gives

x3=x1x2+Dy1y22, y3=x1y2+x2y12.x_3=\dfrac{x_1x_2+Dy_1y_2}{2},\ y_3=\dfrac{x_1y_2+x_2y_1}{2}.

For i=1,2i=1,2, xi≡xi2≡Dyi2≡Dyi(mod2)x_i\equiv x_i^2\equiv Dy_i^2\equiv Dy_i\pmod 2, so

2x3=x1x2+Dy1y2≡D2y1y2+Dy1y2=D(D+1)y1y2≡0(mod2),2y3=x1y2+x2y1≡Dy1y2+Dy2y1=2Dy1y2≡0(mod2).\begin{aligned} 2x_3 &= x_1x_2+Dy_1y_2 \equiv D^2y_1y_2+Dy_1y_2=D(D+1)y_1y_2 \equiv 0 \pmod 2,\\ 2y_3 &= x_1y_2+x_2y_1 \equiv Dy_1y_2+Dy_2y_1 = 2Dy_1y_2 \equiv 0 \pmod 2. \end{aligned}

Thus x3x_3 and y3y_3 are integers. Multiplicativity of the norm then shows that (x3,y3)(x_3,y_3) solves x2−Dy2=±4x^2-Dy^2=\pm 4.

Combining these facts and repeating the arguments of the preceding sections proves the correctness of the algorithm for x2−Dy2=±4x^2-Dy^2=\pm 4. These results show that x2−Dy2=±4x^2-Dy^2=\pm 4 has a simple solution structure similar to that of x2−Dy2=±1x^2-Dy^2=\pm 1: all solutions can be represented by its smallest positive integer solution, without solving any other equation.

In fact, all solutions of x2−Dy2=±1x^2-Dy^2=\pm 1 can be found among the solutions of x2−Dy2=±4x^2-Dy^2=\pm 4, so in this sense x2−Dy2=±4x^2-Dy^2=\pm 4 is more fundamental. Clearly, (x,y)(x,y) solves x2−Dy2=±1x^2-Dy^2=\pm 1 if and only if (2x,2y)(2x,2y) solves x2−Dy2=±4x^2-Dy^2=\pm 4. The preceding analysis shows that when D≡2,3(mod4)D\equiv 2,3\pmod 4, every solution of x2−Dy2=±4x^2-Dy^2=\pm 4 is even in both coordinates, and therefore corresponds to a solution of x2−Dy2=±1x^2-Dy^2=\pm 1.

When D≡0(mod4)D\equiv 0\pmod 4, a solution (x,y)(x,y) of x2−Dy2=±4x^2-Dy^2=\pm 4 always has even xx, but yy may be odd. If y1y_1 is even in the smallest positive solution (x1,y1)(x_1,y_1), then yy is even for every solution, and these integer solutions correspond one-to-one with the integer solutions of x2−Dy2=±1x^2-Dy^2=\pm 1. If y1y_1 is odd, the parity of yky_k agrees with kk and alternates, so only even kk correspond to solutions of x2−Dy2=±1x^2-Dy^2=\pm 1. If y1y_1 is odd in the smallest positive solution (x1,y1)(x_1,y_1) of x2−Dy2=±4x^2-Dy^2=\pm 4 and the norm of x1+y1Dx_1+y_1\sqrt{D} is −4-4, then for such DD, the equation x2−Dy2=±4x^2-Dy^2=\pm 4 has the solution x2−Dy2=−4x^2-Dy^2=-4, but x2−Dy2=−1x^2-Dy^2=-1 does not.

When D≡1(mod4)D\equiv 1\pmod 4, a solution (x,y)(x,y) of x2−Dy2=±4x^2-Dy^2=\pm 4 may be both odd or both even. If the smallest positive solution (x1,y1)(x_1,y_1) 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=±1x^2-Dy^2=\pm 1. If the smallest positive solution (x1,y1)(x_1,y_1) is odd in both coordinates, we have the following result:

Theorem

Let (x1,y1)(x_1,y_1) be the smallest positive integer solution of x2−Dy2=±4x^2-Dy^2=\pm 4. If x1x_1 and y1y_1 are both odd, then D≡5(mod8)D\equiv 5\pmod 8, and an integer solution (x,y)(x,y) of this equation is even in both coordinates if and only if

x+yD2=±(x1+y1D2)3k,k∈Z.\dfrac{x+y\sqrt{D}}{2} = \pm\left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^{3k},k\in\mathbf Z.
Proof

Reducing both sides of x12−Dy12=±4x_1^2-Dy_1^2=\pm 4 modulo 88 gives D≡5(mod8)D\equiv 5\pmod 8. To prove the second conclusion, first show that (x3,y3)(x_3,y_3) are both even. Since

x3+y3D2=(x1+y1D2)3=x13+3Dxy128+3x12y1+Dy138D,\dfrac{x_3+y_3\sqrt{D}}{2} = \left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^{3} = \dfrac{x_1^3+3Dxy_1^2}{8}+\dfrac{3x_1^2y_1+Dy_1^3}{8}\sqrt{D},

it is enough to prove that the right-hand side is integral. The square of an odd number is congruent to 11 modulo 88, so

x13+3Dxy1=x1(x12+3Dy1)≡x1(1+3×5×1)=16x1=0(mod8),3x12y1+Dy13=y1(3x12+Dy12)≡y1(3×1+5×1)=8y1=0(mod8).\begin{aligned} &x_1^3+3Dxy_1 = x_1(x_1^2+3Dy_1) \equiv x_1(1+3\times 5\times 1) = 16x_1 = 0 \pmod 8,\\ &3x_1^2y_1+Dy_1^3 = y_1(3x_1^2+Dy_1^2) \equiv y_1(3\times 1+5\times 1) = 8y_1 = 0 \pmod 8. \end{aligned}

Hence x3,y3x_3,y_3 are both even. Therefore, for every k∈Zk\in\mathbf Z,

x+yD2=±(x1+y1D2)3k=±(x3+y3D2)k∈Z,\dfrac{x+y\sqrt{D}}{2} = \pm\left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^{3k} = \pm\left(\dfrac{x_3+y_3\sqrt{D}}{2}\right)^k \in \mathbf Z,

so (x,y)(x,y) is even in both coordinates. Conversely, for r=1,2r=1,2,

±(x1+y1D2)3k+r=±(x3+y3D2)k(xr+yrD2).\pm\left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^{3k+r} = \pm\left(\dfrac{x_3+y_3\sqrt{D}}{2}\right)^k\left(\dfrac{x_r+y_r\sqrt{D}}{2}\right).

To prove that the corresponding (x,y)(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=1r=1, this is the given condition. For r=2r=2,

x2+y2D2=(x1+y1D2)2=x12+Dy124+x1y12D,\dfrac{x_2+y_2\sqrt{D}}{2} = \left(\dfrac{x_1+y_1\sqrt{D}}{2}\right)^2 = \dfrac{x_1^2+Dy_1^2}{4} + \dfrac{x_1y_1}{2}\sqrt{D},

and x12+Dy12≡1+1×1=2(mod4)x_1^2+Dy_1^2\equiv 1+1\times 1=2\pmod 4, while x1y1≡1(mod2)x_1y_1\equiv 1\pmod 2, so this expression is also not integral. Thus only powers divisible by 33 give solutions that are even in both coordinates.

In other words, one out of every three solutions of x2−Dy2=±4x^2-Dy^2=\pm 4 is even in both coordinates, and corresponds to an integer solution of x2−Dy2=±1x^2-Dy^2=\pm 1. This also shows that, for D≡1(mod4)D\equiv 1\pmod 4, x2−Dy2=−4x^2-Dy^2=-4 has a solution if and only if x2−Dy2=−1x^2-Dy^2=-1 has a solution.

The discussion so far is enough to compute the fundamental units of a real quadratic integer ring. Let DD be a positive integer without a square factor. For D≡2,3(mod4)D\equiv 2,3\pmod 4, it is enough to find the smallest positive integer solution of x2−Dy2=±1x^2-Dy^2=\pm 1; for D≡1(mod4)D\equiv 1\pmod 4, it is enough to find the smallest positive integer solution of x2−Dy2=±4x^2-Dy^2=\pm 4. Given the smallest positive solution (x,y)(x,y), the fundamental units are ±x±yD\pm x\pm y\sqrt{D} for D≡2,3(mod4)D\equiv 2,3\pmod 4, and ±x±yD2\dfrac{\pm x\pm y\sqrt{D}}{2} for D≡1(mod4)D\equiv 1\pmod 4.

Example
  1. Solve x2−14y2=±4x^2-14y^2=\pm 4.

    The preceding example shows that the smallest positive integer solution of x2−14y2=4x^2-14y^2=4 is (30,8)(30,8), while x2−14y2=−4x^2-14y^2=-4 has no solution.

  2. Solve x2−41y2=±4x^2-41y^2=\pm 4.

    Running the PQa algorithm from (P0,Q0,D)=(1,2,41)(P_0,Q_0,D)=(1,2,41) gives the following results (the red entries form the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    00112233331155−16-16
    1155881\color{red}{1}44117788
    2233442\color{red}{2}1111331919−8-8
    3355442\color{red}{2}26267745451616
    4433881\color{red}{1}373710106464−4-4
    5555225\color{red}{5}21121157573653651616
    665588112482486767429429−8-8
    773344227077071911911223122388
    885544221662166244944928752875−16-16
    99338811236923696406404098409844
    10105522551350713507364936492336523365−16-16
    1111558811158761587642894289274632746388

    The period length ℓ=5\ell=5 is odd. The minimal positive integer solution of x2−41y2=−4x^2-41y^2=-4 is (G4,B4)=(64,10)(G_4,B_4)=(64,10), and the minimal positive integer solution of x2−41y2=4x^2-41y^2=4 is (G9,B9)=(4098,640)(G_9,B_9)=(4098,640). They satisfy:

    4098+640412=(64+10412)2.\dfrac{4098+640\sqrt{41}}{2} = \left(\dfrac{64+10\sqrt{41}}{2}\right)^2.

    Of course, because D≡1(mod8)D\equiv 1\pmod 8, the conclusions above show that the minimal positive integer solutions of x2−41y2=±4x^2-41y^2=\pm 4 in this case are always even, and are always the minimal positive integer solutions of x2−41y2=±1x^2-41y^2=\pm 1 multiplied by 22. Thus, the example above can also be used directly.

  3. Solve the equation x2−13y2=±4x^2-13y^2=\pm 4.

    Running the PQa algorithm with (P0,Q0,D)=(1,2,13)(P_0,Q_0,D)=(1,2,13) gives the following result (the red portion is the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    00112222221133−4-4
    1133223\color{red}{3}7733111144
    22332233232310103636−4-4
    333322337474333311911944

    The period length ℓ=1\ell=1 is odd. The minimal positive integer solution of x2−13y2=−4x^2-13y^2=-4 is (G0,B0)=(3,1)(G_0,B_0)=(3,1), and the minimal positive integer solution of x2−13y2=4x^2-13y^2=4 is (G1,B1)=(11,3)(G_1,B_1)=(11,3).

    Because the minimal positive integer solutions of this equation are all odd, the pair (G2,B2)=(36,10)(G_2,B_2)=(36,10) at the end of the third period can be used to obtain the corresponding (negative) Pell equation x2−13y2=±1x^2-13y^2=\pm 1 with minimal positive integer solution (18,5)(18,5). It can also be obtained directly:

    36+10132=(3+132)3.\dfrac{36+10\sqrt{13}}{2}=\left(\dfrac{3+\sqrt{13}}{2}\right)^3.

    Moreover, this is a solution of the negative Pell equation. The minimal positive integer solution of the corresponding Pell equation is (649,180)(649,180).

  4. Solve the equation x2−52y2=±4x^2-52y^2=\pm 4.

    Because the minimal positive integer solutions of x2−13y2=±1x^2-13y^2=\pm 1 are (18,5)(18,5) and (649,180)(649,180), the minimal positive integer solutions of x2−52y2=±4x^2-52y^2=\pm 4 are (36,10)(36,10) and (1298,360)(1298,360).

General case

Finally, we discuss how to solve the generalized Pell equation.

For the case ∣N∣<D|N|<\sqrt{D}, there is a simple solution. The conclusions above show that the equation x2−Dy2=Nx^2-Dy^2=N has a solution (x,y)(x,y), which must satisfy that xy\dfrac{x}{y} is a convergent of D\sqrt{D}. Moreover, from the structure of solutions discussed above, every fundamental solution (x,y)(x,y) satisfies x+yDx+y\sqrt{D} no greater than the corresponding fundamental solution of the Pell equation x2−Dy2=1x^2-Dy^2=1, namely x1+y1Dx_1+y_1\sqrt{D}. By the monotonicity of the denominator sequence BkB_k 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)(P_0,Q_0,D)=(0,1,D) until Qℓ′=1Q_{\ell'}=1 and ℓ′\ell' is even. During this process, for every (Ak,Bk)(A_k,B_k) that appears, check whether there is an integer ff such that

Ak2−DBk2=(−1)k+1Qk+1=N/f2A_k^2-DB_k^2 = (-1)^{k+1}Q_{k+1} = N/f^2

If this holds, record (fAk,fBk)(fA_{k},fB_{k}) as a minimal positive integer solution. All (fAk,fBk)(fA_k,fB_k) recorded in this process are exactly the minimal positive integer solutions of x2−Dy2=Nx^2-Dy^2=N. Using (Aℓ′−1,Bℓ′−1)(A_{\ell'-1},B_{\ell'-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 ℓ\ell is even or odd, the ℓ′\ell' above may be either ℓ\ell or 2ℓ2\ell.

For a more general NN, the above method no longer applies. First enumerate all square factors of NN, writing each as f2f^2; let m=N/f2m=N/f^2, and enumerate all solutions to the congruence z2≡D(mod∣m∣)z^2\equiv D\pmod{|m|} satisfying −∣m∣/2<z≤∣m∣/2-|m|/2<z \le |m|/2, denoting each solution by zz. Then run the PQa algorithm with (P0,Q0,D)=(z,∣m∣,D)(P_0,Q_0,D)=(z,|m|,D) until Qk=±1Q_k=\pm 1 or one period has ended. In the second case, the equation associated with this pair (f,z)(f,z) has no solution. In the first case, further determine whether (−1)kQk=N/∣N∣(-1)^kQ_k=N/|N|. If the signs agree, (fGk−1,fBk−1)(fG_{k-1},fB_{k-1}) is a solution of x2−Dy2=Nx^2-Dy^2=N. Otherwise, it is a solution of x2−Dy2=−Nx^2-Dy^2=-N, and it can be composed with the fundamental solution of the corresponding negative Pell equation to obtain a solution of x2−Dy2=Nx^2-Dy^2=N if and only if that negative Pell equation has a solution. After traversing all pairs (f,z)(f,z), we obtain exactly one solution from each equivalence class of x2−Dy2=Nx^2-Dy^2=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=Nx^2-Dy^2=N has an integer solution (x,y)(x,y) with x≥0,y>0,gcd⁡(x,y)=1x\ge 0, y>0,\gcd(x,y)=1. Let Q0=∣N∣Q_0=|N|, so gcd⁡(Q0,y)=1\gcd(Q_0,y)=1. Let P0P_0 be a solution of the congruence x≡−P0y(modQ0)x\equiv -P_0y\pmod{Q_0} with −Q0/2<P0≤Q0/2-Q_0/2<P_0\le Q_0/2, and let the integer XX satisfy x=Q0X−P0yx=Q_0X-P_0y. Then, P02≡D(modQ0)P_0^2\equiv D\pmod{Q_0}, and Xy\dfrac{X}{y} is a convergent of ω=P0+DQ0\omega=\dfrac{P_0+\sqrt{D}}{Q_0}, namely Ak−1Bk−1\dfrac{A_{k-1}}{B_{k-1}}, with Qk=(−1)kN∣N∣Q_k=(-1)^k\dfrac{N}{|N|}.

Proof

Using x≡−P0y(modQ0)x\equiv -P_0y\pmod{Q_0} and x2−Dy2=N≡0(modQ0)x^2-Dy^2=N\equiv 0\pmod{Q_0}; it is clear that P02≡D(modQ0)P_0^2\equiv D\pmod{Q_0}. Therefore,

P0x+Dy≡−P02y+Dy=(D−P02)y≡0(modQ0).P_0x+Dy\equiv -P_0^2y+Dy = (D-P_0^2)y\equiv 0\pmod{Q_0}.

Thus, consider the integer matrix

(PRQS)=(XP0x+DyQ0yx).\begin{pmatrix}P & R \\ Q & S\end{pmatrix} = \begin{pmatrix}X & \dfrac{P_0x+Dy}{Q_0} \\ y & x\end{pmatrix}.

Its determinant is

PS−QR=x(x+P0y)−y(P0x+Dy)Q0=x2−Dy2Q0=±1.PS-QR = \dfrac{x(x+P_0y)-y(P_0x+Dy)}{Q_0} = \dfrac{x^2-Dy^2}{Q_0} = \pm 1.

Moreover, let ζ=D>1\zeta =\sqrt{D} > 1; then

Pζ+RQζ+S=(x+P0y)D+(P0x+Dy)(x+yD)Q0=P0+DQ0=ω.\dfrac{P\zeta+R}{Q\zeta+S} = \dfrac{(x+P_0y)\sqrt{D}+(P_0x+Dy)}{(x+y\sqrt{D})Q_0} = \dfrac{P_0+\sqrt{D}}{Q_0} = \omega.

We now prove that PQ\dfrac{P}{Q} is a convergent of ω\omega. Without loss of generality, suppose PQ\dfrac{P}{Q} has continued fraction expansion

PQ=[a0,a1,⋯ ,ak]\dfrac{P}{Q} = [a_0,a_1,\cdots,a_k]

and PS−QR=(−1)k−1PS-QR = (-1)^{k-1}. Suppose pkqk\dfrac{p_k}{q_k} is its kk-th convergent. Then (pk,qk)=(P,Q)(p_k,q_k)=(P,Q), and the difference formula for convergents shows that pkqk−1−qkpk−1=(−1)k−1p_kq_{k-1}-q_kp_{k-1}=(-1)^{k-1}. This shows that

pk(S−qk−1)=qk(R−pk−1).p_k(S-q_{k-1}) = q_k(R-p_{k-1}).

Consider the following cases:

  • If S=0S=0, it is easy to verify Q=R=1Q=R=1, so ω=P+ζ−1=[P,ζ]\omega=P+\zeta^{-1}=[P,\zeta], and therefore PQ=P\dfrac{P}{Q}=P is a convergent of ω\omega with index 00;
  • If Q=S>0Q=S>0, then Q=S=1Q=S=1 and P−R=±1P-R=\pm 1. In this case,
    • If P=R+1P=R+1, then ω=R+11+ζ−1=[R,1,ζ]\omega=R+\dfrac{1}{1+\zeta^{-1}}=[R,1,\zeta], so PQ=R+11=[R,1]\dfrac{P}{Q}=\dfrac{R+1}{1}=[R,1] is a convergent of ω\omega with index 11;
    • If P=R−1P=R-1, then ω=R−1+11+ζ=[R−1,ζ−1]\omega=R-1+\dfrac{1}{1+\zeta}=[R-1,\zeta-1], so PQ=R−1\dfrac{P}{Q}=R-1 is a convergent of ω\omega with index 00;
  • If Q≠S>0Q\neq S>0, then, since Q=qk∣(S−qk−1)Q=q_k\mid(S-q_{k-1}), there is always an integer κ\kappa such that S=κqk+qk−1S=\kappa q_k+q_{k-1} and R=κpk+pk−1R=\kappa p_k+p_{k-1}. Since qk≥qk−1q_k\ge q_{k-1} and S>0S>0, we have κ≥0\kappa\ge 0. Therefore, ω=(κ+ζ)pk+pk−1(κ+ζ)qk+qk−1=[a0,a1,⋯ ,ak,κ+ζ]\omega=\dfrac{(\kappa+\zeta)p_k+p_{k-1}}{(\kappa+\zeta)q_k+q_{k-1}}=[a_0,a_1,\cdots,a_k,\kappa+\zeta], so PQ\dfrac{P}{Q} is its kk-th convergent.

In summary, Xy\dfrac{X}{y} is a convergent of ω=P0+DQ0\omega=\dfrac{P_0+\sqrt{D}}{Q_0}, denoted by Ak−1Bk−1\dfrac{A_{k-1}}{B_{k-1}}. Since Ak−12−DBk−12=(−1)kQ0QkA_{k-1}^2-DB_{k-1}^2=(-1)^kQ_0Q_k, so Qk=(−1)kN∣N∣Q_k=(-1)^k\dfrac{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∣(-1)^kQ_k=N/|N|. Unlike all the preceding algorithms, here the qualifying kk may occur before the period has been entered.

Example
  1. Solve the equation x2−157y2=12x^2-157y^2=12.

    Since 122<15712^2<157, running the PQa algorithm with (P0,Q0,D)=(0,1,157)(P_0,Q_0,D)=(0,1,157) gives the following result (the red portion is the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    00001112121212111212−13-13
    11121213131\color{red}113131113131212
    221112121\color{red}12525222525−3-3
    331111337\color{red}718818815151881881919
    44101019191\color{red}12132131717213213−4-4
    5599445\color{red}5125312531001001253125399
    661111992\color{red}22719271921721727192719−12-12
    777712121\color{red}139723972317317397239721111
    885511111\color{red}16691669153453466916691−11-11
    996611111\color{red}1106631066385185110663106631212
    10105512121\color{red}11735417354138513851735417354−9-9
    111177992\color{red}2453714537136213621453714537144
    12121111445\color{red}52442092442091949019490244209244209−19-19
    13139919191\color{red}1289580289580231112311128958028958033
    14141010337\color{red}72271269227126918126718126722712692271269−12-12
    1515111112121\color{red}125608492560849204378204378256084925608491313
    16161113131\color{red}14832118483211838564538564548321184832118−1-1
    171712121124\color{red}{24}118531681118531681945985894598581185316811185316811313
    1818121213131112336379912336379998455039845503123363799123363799−12-12
    191911121211241895480241895480193053611930536124189548024189548033
    2020111133771816632159181663215914498303014498303018166321591816632159−19-19
    21211010191911205852763920585276391642883911642883912058527639205852763944
    222299445512109270354121092703549664249859664249851210927035412109270354−9-9
    23231111992226277068347262770683472097138361209713836126277068347262770683471212
    2424771212113838633870138386338701306356334630635633463838633870138386338701−11-11
    25255511111164663407048646634070485160701707516070170764663407048646634070481111
    26266611111110304974574910304974574982242650538224265053103049745749103049745749−12-12
    272755121211167713152797167713152797133849667601338496676016771315279716771315279799
    28287799224384760513434384760513433499419857334994198573438476051343438476051343−4-4
    29291111445523600934095122360093409512188355959625188355959625236009340951223600934095121919
    3030991919112798569460855279856946085522335015819822335015819827985694608552798569460855−3-3
    31311010337721950079635497219500796354971751807067011175180706701121950079635497219500796354971212
    323211111212112474864909635224748649096352197515722520919751572252092474864909635224748649096352−13-13
    333311131311466987287318494669872873184937269642922203726964292220466987287318494669872873184911
    3434121211242411455181386607281145518138660728914223002384899142230023848911455181386607281145518138660728−13-13
    35351212131311119221686739257711922168673925779514926453070995149264530709119221686739257711922168673925771212

    The period length ℓ=17\ell=17 is odd, so within two periods we need to consider Gk−12−157Bk−12G_{k-1}^2-157B_{k-1}^2 differs from 1212 by a square factor, namely k=1,9,13,19,23,31k=1,9,13,19,23,31; the corresponding solutions are the values of (fG,fB)(fG,fB):

    kkfffGk−1fG_{k-1}fBk−1fB_{k-1}xxyy
    1111131311131311
    991110663106638518511066310663851851
    13132257916057916046222462225791605791604622246222
    1919224837909604837909603861072238610722−579160-5791604622246222
    232311262770683472627706834720971383612097138361−10663-10663851851
    313111219500796354972195007963549717518070670111751807067011−13-1311

    All (fG,fB)(fG,fB) are the minimal positive integer solutions in all equivalence classes of x2−157y2=12x^2-157y^2=12. To obtain all solutions from these, we can use the fundamental solution of the corresponding Pell equation (46698728731849,3726964292220)(46698728731849,3726964292220). For example, they can be transformed into the fundamental solutions (x,y)(x,y), and the corresponding solutions are also listed in the table above.

  2. Solve the equation x2−157y2=12x^2-157y^2=12.

    This time, use the Lagrange–Matthews–Mollin algorithm to solve it. First, enumerate N=12N=12 square factors:

    • f2=12f^2=1^2 gives m=12m=12, and the congruence P2≡157(mod12)P^2\equiv 157\pmod{12} has solutions z=±1,±5z=\pm 1,\pm 5;
    • f2=22f^2=2^2 gives m=3m=3, and the congruence P2≡157(mod3)P^2\equiv 157\pmod{3} has solutions z=±1z=\pm 1.

    For every possible (f,z)(f,z) combination, run the PQa algorithm with initial parameters (P0,Q0,D)=(z,∣m∣,D)(P_0,Q_0,D)=(z,|m|,D) and find the first occurrence of (−1)kQk=1(-1)^kQ_k=1; the corresponding (fGk−1,fBk−1)(fG_{k-1},fB_{k-1}) is a solution. The results are shown in the following table:

    ffzzmmkkfGk−1fG_{k-1}fBk−1fB_{k-1}
    111112123232219500796354972195007963549717518070670111751807067011
    11−1-1121222131311
    115512122424262770683472627706834720971383612097138361
    11−5-5121210101066310663851851
    22113320204837909604837909603861072238610722
    22−1-13314145791605791604622246222

    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.

  3. Solve the equation x2−79y2=±101x^2-79y^2=\pm 101.

    Again, use the Lagrange–Matthews–Mollin algorithm to solve it. Since N=101N=101 is prime, so necessarily f=1f=1. Then, m=101m=101, the corresponding congruence P2≡79(mod101)P^2\equiv 79\pmod{101} has solutions P=±33P=\pm 33.

    Running the PQa algorithm with (P0,Q0,D)=(33,101,79)(P_0,Q_0,D)=(33,101,79) gives the following result (the red portion is the first period):

    kkPPQQaaAABBGGG2−DB2G^2-DB^2
    003333101101000011−33-3310101010
    11−33-33−10-102211223535909909
    221313992222553737−606-606
    3355662\color{red}2551212109109505505
    4477553\color{red}317174141364364−303-303
    5588335\color{red}590902172171929192910101010
    667710101\color{red}110710725825822932293−707-707
    7733771\color{red}119719747547542224222909909
    8844991\color{red}130430473373365156515−606-606
    99556622805805194119411725217252505505

    The period length ℓ=6\ell=6 is even. There is no Qk=±1Q_k=\pm 1, so this case has no solution. Similarly, running the PQa algorithm with (P0,Q0,D)=(−33,101,79)(P_0,Q_0,D)=(-33,101,79) also shows a similar situation. Therefore, this equation has no solution.

Exercises

References and notes

Footnotes

  1. When DD is a perfect square, direct factorization gives (x+yD)(x−yD)=N(x+y\sqrt{D})(x-y\sqrt{D})=N, so all solutions can be found by enumerating the factors of NN. In particular, when N=1N=1, the equation has only the solutions (±1,0)(\pm 1,0); when N=−1N=-1 and D≠1D\neq 1, the equation has no solution. ↩

  2. Some Chinese sources also call it the negative Pell equation of the second type. ↩

  3. That is, a rational number of the form n+12n+\dfrac12 with n∈Zn\in\mathbf Z. ↩

  4. 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+yDx+y\sqrt{D} has half-integer x,yx,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,yx,y to both be positive. ↩

  5. A relatively practical test and tool can be found here and in its references. The equation x2−Dy2=−1x^2-Dy^2=-1 has a solution for exactly the positive integers DD listed in OEIS A031396. ↩

This content comes from OI Wiki: Pell Equation. See the original for authors and revision history. AIOJ has adapted the formatting, in-site links, and resource paths.

Content is shared under CC BY-SA 4.0 and SATA license; additional terms are governed by the original notice. Thanks to the OI Wiki authors and contributors; per the SATA terms, please visit the OI Wiki repository and star it to show your support.