solving pell’s equation using the nearest square …abstract this talk is about the nearest square...

38
Solving Pell’s equation using the nearest square continued fraction Keith Matthews 12th June 2008

Upload: others

Post on 23-May-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Solving Pell’s equationusing the nearest square continued fraction

Keith Matthews

12th June 2008

Abstract

This talk is about the nearest square continued fraction of A.A.K.Ayyangar (1941) and its use in finding the smallest positivesolution of Pell’s equation x2 − Dy2 = ±1.

Contrary to the 1944 review by D.H. Lehmer, its ”slight blemishes”are indeed compensated by the its period length being about 70%of that of the regular continued fraction of

√D and also having a

3-case mid-point criterion for solving Pell’s equation.

Hugh Williams and Peter Buhr (1979) gave a 6-case midpointcriterion in terms of Hurwitz’ continued fraction of the first kind.Their paper was rather complicated.

It was after studying their paper that Jim White and JohnRobertson came up with a 3-case midpoint criterion using thenearest square continued fraction.

Euler (1765) gave a two-case midpoint criterion for solving Pell’sequation x2 − Dy2 = ±1 using the regular continued fraction(RCF) expansion of

√D.

√D = a0 +

1

a1 +1

a2 + · · ·

where a0 = b√

Dc and ai ≥ 1 for all i .We write √

D = [a0, a1, a2, . . .].

The n-th convergent is defined by

An/Bn = a0 +1

a1 +1

. . . +1

an−1 +1

an

An and Bn can be computed recursively:

A0 = a0,B0 = 1,A1 = a0a1 + 1,B1 = a1,

Ai+1 = ai+1Ai + Ai−1

Bi+1 = ai+1Bi + Bi−1,

for i ≥ 1.

The RCF for√

D is periodic with period-length k:

√D =

{[a0, a1, . . . , ah−1, ah−1, . . . , a1, 2a0] if k = 2h − 1,[a0, a1, . . . , ah−1, ah, ah−1, . . . , a1, 2a0] if k = 2h.

The smallest positive integer solution of x2−Dy2 = ±1 is given by

(x , y) = (Ak−1,Bk−1),

where An/Bn is the n-th convergent to√

D.

Euler observed that if k = 2h − 1,

Ak−1 = Ah−1Bh−1 + Ah−2Bh−2

Bk−1 = B2h−1 + B2

h−2,

while if k = 2h,

Ak−1 = Bh−1(Ah + Ah−2) + (−1)h

Bk−1 = Bh−1(Bh + Bh−2).

Also ifξn = (Pn +

√D)/Qn = [an, an+1, . . .]

is the n-th complete quotient of the RCF expansion of√

D, thenthe equations

Qh = Qh−1 if k = 2h − 1,Ph = Ph+1 if k = 2h,

enable us to determine h, as Qv = Qv+1 (k odd) and Pv = Pv+1,(k even), 1 ≤ v < k imply k = h.

The nearest square continued fraction (NSCF) of a quadratic surdξ0 was introduced by A.A.K. Ayyangar (AAK) in 1940 and basedon the cyclic method of solving Pell’s equation due to Bhaskara in1150.The NSCF is a half-regular continued fraction. ie.

ξ0 = a0 +ε1

a1 +ε2

a2 + · · ·

where the ai are integers and

ai ≥ 1, εi = ±1 and ai + εi+1 ≥ 1 if i ≥ 1.

We write the continued fraction as

ξ0 = a0 +ε1||a1

+ε2||a2

+ · · ·

Let ξ0 = P+√

DQ be a quadratic surd in standard form.

ie. D is a non-square positive integer and P,Q 6= 0, D−P2

Q areintegers, having no common factor other than 1.

Then with a = bξ0c, the integer part of ξ0, we can represent ξ0 inone of two forms (positive or negative representations)

P +√

D

Q= a +

Q ′

P ′ +√

D= a + 1− Q

′′

P ′′ +√

D,

where P′+√

DQ′ > 1 and P

′′+√

DQ′′

> 1 are also standard surds.

These equations imply

(1) P ′ = aQ − P; (2) P′′

= (a + 1)Q − P,

(3) P ′2 = R − QQ ′; (4) P′′2

= R + QQ′′.

These in turn imply

(5) P′′ − P ′ = Q and (6) P

′′+ P ′ = Q ′ + Q

′′.

We get P ′ and Q ′ from (1) and (3), then P′′

and Q′′

from (5) and(6), respectively.

AAK chose the partial denominator a0 and numerator ε1 of thenew continued fraction development as follows:

(a) a0 = a if |Q ′| < |Q ′′ |, or |Q ′| = |Q ′′ | and Q < 0,

(b) a0 = a + 1 if |Q ′| > |Q ′′ |, or |Q ′| = |Q ′′ | and Q > 0.

Alsoε1 = 1 and ξ1 = P′+

√D

Q′ in case (a),

ε1 = −1 and ξ1 = P′′+√

DQ′′

in case (b).

Then ξ0 = a0 + ε1ξ1

and

ε1 = ±1, a0 is an integer and ξ1 = P1+√

DQ1

> 1.

We proceed similarly with ξ1 and so on:

ξn = an +εn+1

ξn+1

and

ξ0 = a0 +ε1||a1

+ε2||a2

+ · · ·

ξn+1 is called the successor of ξn.

Relations analogous to those for regular continued fractions holdfor Pn,Qn and an, n ≥ 0:

Pn+1 + Pn = anQn

P2n+1 + εn+1QnQn+1 = D.

The |Qn| successively diminish as long as |Qn| >√

D and soeventually, we have |Qn| <

√D. When this stage is reached,

0 < Pi < 2√

D and 0 < Qi <√

D for i ≥ n + 1.

This implies eventual periodicity of the complete quotients andhence the partial quotients.

AAK defines ξv to be a special surd if

Q2v−1 + 1

4Q2v ≤ D, Q2

v + 14Q2

v−1 ≤ D.

A semi-reduced surd is the successor of a special surd.A reduced surd to the successor of a semi-reduced surd.

Properties:

1. A semi-reduced surd is a special surd.

2. A quadratic surd has a purely periodic NSCF expansion if andonly if it is reduced.

3. If ξv is reduced, then Pv > 0,Qv > 0 and av ≥ 2.

Examples: (i)p+q+

√p2+q2

p , p > 2q > 0, gcd(p, q) = 1,

(ii) the successor of√

D.

The NSCF development of√

D has the form

√D = a0 +

ε1||a1∗

+ · · · εk ||2a0∗

, (1)

where the asterisks denote a period of length k and ξp = ξp+k ,εp = εp+k , ap = ap+k for p ≥ 1.

Note: a0 is the nearest integer to√

D.

There are two types of NSCF expansions of√

D:

(I) No complete quotient of the cycle has the formp+q+

√p2+q2

p ,where p > 2q > 0, gcd(p, q) = 1.This type possesses the classical symmetries of the regularcontinued fraction if k > 1:

av = ak−v (1 ≤ v ≤ k − 1)Qv = Qk−v (1 ≤ v ≤ k − 1)εv = εk+1−v (1 ≤ v ≤ k)Pv = Pk+1−v (1 ≤ v ≤ k).

Note: If k = 2h + 1, then Qh = Qh+1.Conversely Qv = Qv+1, 1 ≤ v < k implies v = h.

If k = 2h, then Ph = Ph+1.Conversely Pv = Pv+1, 1 ≤ v < k implies v = h.

Examples.√73 = 9− 1|

|2∗

+ 1||5 + 1|

|5 + 1||2 −

1||18∗

. (odd period)√

19 = 4 + 1||3∗− 1||5 −

1|3 + 1|

|8∗

. (even period)√

n2 + 1 = n + 1||2n∗

(n ≥ 1),√

n2 − 1 = n − 1||2n∗

(n > 1).

(II) There is one complete quotient ξh in the cycle of the formp+q+

√p2+q2

p , where p > 2q > 0, gcd(p, q) = 1. In this case k ≥ 4is even and h = k/2. This type also possesses the symmetries ofType I, apart from a central set of three unsymmetrical terms:

a k2

= 2, ε k2

= −1, ε k2+1 = 1, a k

2−1 = a k

2+1 + 1.

√D = a0 +

ε1||a1∗

+ · · ·+ε k

2−1||a k

2−1

− 1||2

+1|

|a k2−1 − 1

+ · · ·+ εk ||2a0∗

.

For example√

29 = 5 + 1||3∗− 1||2 + 1|

2 + 1||10∗

.

Other examples are 53, 58, 85, 97.

En is the number of D < 10n of Type I with even period.

On is the number of D < 10n of Type I with odd period.

Fn is the number of D < 10n of Type II.

Nn is the number of D < 10n.

n En On Fn Nn

2 60 25 5 903 762 165 42 9694 8252 1266 382 99005 85856 10465 3363 996846 878243 90533 30224 999000

Note: Pv 6= Pv+1, 1 ≤ v < k .

εh = −1, Qh−1 is even and Ph = Qh + 12Qh−1

(observed by John Robertson and Jim White).

Conversely if εv = −1, Qv−1 is even andPv = Qv + 1

2Qv−1, 1 ≤ v < k , then D is of Type II and v = h.

For both types I and II, we have Qk = 1. For

√D = a0 +

ε1Q1

P1 +√

D

= a0 +ε1Q1(P1 −

√D)

P21 − D

= a0 − P1 +√

D.

Hence P1 = a0. Then

P1 = Pk (symmetry)

P1 = Pk+1 (periodicity)

2a0 = 2P1 = Pk + Pk+1

= akQk

= 2a0Qk .

Note: ξk = Pk+√

DQk

= a0 +√

D.

A classical result for a half-regular expansion of ξ0 = P0+√

DQ0

is

A2n − DB2

n = (−1)n+1(ε1ε2 · · · εn+1)Qn+1Q0.

In the special case ξ0 =√

D, where Q0 = 1 = Qk , we have

A2k−1 − DB2

k−1 = (−1)kε1ε2 · · · εk .

Also by periodicity, Qn = 1 if k divides n.

Conversely, suppose Qn = 1, n ≥ 1.

Then ξn = Pn +√

D.

We prove Pn = [√

D] = a0, the nearest integer to√

D.

Then ξn = a0 +√

D = ξk and k divides n.

We start with P2n + εnQn−1Qn = D, noting that Qn−1 > 0,Qn > 0.

Case 1. Pn >√

D. Then εn = −1.

P2n − D = Qn−1 <

√D (ξn is reduced)

0 < Pn −√

D <

√D

Pn +√

D<

√D

2√

D=

1

2.

Hence Pn = [√

D].

Case 2. Pn <√

D. Then εn = 1.

Q2n−1 + 1

4Q2

n ≤ D = P2n + Qn−1 (ξn is reduced)

(Qn−1 − 12)2 ≤ P2

n

Qn−1 − 12≤ Pn

Qn−1 ≤ Pn + 12

D − P2n = Qn−1 ≤ Pn

0 <√

D − Pn ≤Pn√

D + Pn

<Pn

2Pn=

1

2.

Again Pn = [√

D].

The convergents Akt−1/Bkt−1, t ≥ 1, in fact give all positiveinteger solutions of Pell’s equation x2 − Dy2 = ±1.

For if x2 − Dy2 = ±1, x > 0, y > 0, we can prove that x/y is anNSCF convergent to

√D, as follows.

It is certainly an RCF convergent.

We now introduce a transformation T1 of Perron, which converts ahalf-regular continued fraction to an RCF:

To get the RCF partial quotients:

Before a negative partial numerator, insert the term +1||1 .

Replace each an, n ≥ 0 by:

(a) an if εn= +1, εn+1= +1,

(b) an − 1 if εn= +1, εn+1= -1, or εn= -1, εn+1= +1,

(c) an − 2 if εn= -1, εn+1= -1.

Here ε0 = 1.

Note: If ξv and ξv+1 are NSCF reduced quadratic surds andεv = −1 and εv+1 = −1, then av ≥ 3.

Hence T1 produces a ”genuine” RCF, ie. with no zero partialquotients.

For n ≥ 0,

(i) εn+1 = −1 gives rise to RCF convergents

A′m−1/B′m−1 = (An −An−1)/(Bn −Bn−1), A′m/B

′m = An/Bn

and RCF complete quotients

P ′m +√

D

Q ′m= ξn+1/(ξn+1 − 1),

P ′m+1 +√

D

Q ′m+1

= ξn+1 − 1.

(ii) εn+1 = 1 gives rise to RCF convergent An/Bn and RCFcomplete quotient ξn+1.

It is not difficult to show that x/y does not have the form(An − An−1)/(Bn − Bn−1) and hence x/y must also be an NSCFconvergent.

Remark. Arguing along these lines shows that the period length ofthe RCF expansion of

√D is k + r , where r is the number of

εn = −1 occurring in the period partial numerators ε1, . . . , εk ofthe NSCF expansion of

√D.

Example. D = 97. The NSCF expansion of√

97 is of type II, withperiod-length 6. There are five εi = −1 in the period range1 ≤ i ≤ 6 and the period-length of the RCF expansion is 11.

j i ξi ξ′j εi ai a′j Ai/Bi A′j /B′j

0 0 0+√

971

0+√

971

1 10 9 10/1 9/1

1 9+√

9716

1 10/1

2 1 10+√

973

7+√

973

−1 7 5 69/7 59/6

3 8+√

9711

1 69/7

4 2 11+√

978

3+√

978

−1 3 1 197/20 128/13

5 5+√

979

1 197/20

6 3 13+√

979

4+√

979

−1 2 1 325/33 325/33

7 4 5+√

978

5+√

978

1 2 1 847/86 522/53

8 3+√

9711

1 847/86

9 5 11+√

973

8+√

973

−1 7 5 5604/569 4757/483

10 7+√

9716

1 5604/569

11 6 10+√

971

9+√

971

−1 20 18 111233/11294 105629/10725

12 9+√

9716

1 111233/11294

13 7 10+√

973

7+√

973

−1 7 5 773027/78489 661794/67195

Exactly one of the following P, Q and PQ tests will apply for anyD > 0, not a square:

P-test: For some h, 1 ≤ h < k , Ph = Ph+1, in which case k = 2hand

Ak−1 = AhBh−1 + εhAh−1Bh−2

Bk−1 = Bh−1(Bh + εhBh−2).

In this case A2k−1 − DB2

k−1 = 1.

Q-test: For some h, 0 ≤ h < k , Qh = Qh+1, in which casek = 2h + 1 and

Ak−1 = AhBh + εh+1Ah−1Bh−1

Bk−1 = B2h + εh+1B

2h−1.

In this case A2k−1 − DB2

k−1 = −εh+1.

PQ-test: For some h, 1 ≤ h < k , Qh−1 is even, Ph = Qh + 12Qh−1

and εh = −1, in which case k = 2h and

Ak−1 = AhBh−1 − Bh−2(Ah−1 − Ah−2)

Bk−1 = 2B2h−1 − BhBh−2.

In this case A2k−1 − DB2

k−1 = −1.

The formulae for Ak−1 and Bk−1 depend on the followingconservation identities which are proved using ”downward”induction on t:

(i) Let k = 2h + 1. Then for Type I and 0 ≤ t ≤ h, we have

A2h = Ah+tBh−t + εh+1+tAh+t−1Bh−t−1

B2h = Bh+tBh−t + εh+1+tBh+t−1Bh−t−1

(ii) Let k = 2h. Then for Type I and 0 ≤ t ≤ h, or Type II withh ≥ 2 and 2 ≤ t ≤ h, we have

A2h−1 = Ah+t−1Bh−t + εh+tAh+t−2Bh−t−1

B2h−1 = Bh+t−1Bh−t + εh+tBh+t−2Bh−t−1

Let π(D) and p(D) respectively denote the periods of the NSCFand RCF expansions of

√D, where D is not a perfect square and

letΠ(n) =

∑D≤n

π(D), P(n) =∑D≤n

p(D).

n Π(n) p(n) Π(n)/P(n)

1000000 152198657 219245100 .69419412000000 417839927 601858071 .69424993000000 755029499 1087529823 .69426094000000 1149044240 1655081352 .69425245000000 1592110649 2293328944 .69423566000000 2078609220 2994112273 .69423227000000 2604125007 3751067951 .69423568000000 3165696279 4559939520 .69424089000000 3760639205 5416886128 .6942437

10000000 4387213325 6319390242 .6942463

There are grounds for believing that

Π(n)/P(n)→log (1+

√5

2 )

log 2= .6942419 · · ·

For D with a long RCF period, we expect π(D)/p(D) to be nearthis value.

For example, D = 26437680473689, Daniel Shanks (1974) andquoted by William Adams (1979).

p(D) = 18331889, π(D) = 12726394, π(D)/p(D) = .6942216 · · ·

This D obeys the PQ-test.

AAK’s paper and a LATEX version are available at

http://www.numbertheory.org/continued fractions.html

BCMATH versions of NSCF and some other continued fractionalgorithms are available at

http://www.numbertheory.org/php/CFRAC.html