Calculating the Convergents of x2 −Dy2 = ±1 (Part XIII)

A Computer Programming Method for Pellian Equations

The Pellian equation is the Diophantine equation x2 − Dy2 = z2 where z equals 1. The least solutions of the Pell equation are posted in Wikipedia and also listed in Table 91, page 254 of Recreations in the Theory of Numbers by Albert H. Beiler (1966), where the values for D on page 252-253 have been computed using the following two expressions:

x = [(p + qD)n + (p − qD)n ∕ 2]
y = [(p + qD)n + (p − q2D)n ∕ 22D)]

The quadratic surd D, however, can be converted to a continued fraction, then into a series of converging fractions yielding the Pell equation. The convergents pn∕qn are calculated from the ans, i.e., the coefficients or terms of a continued fraction (see Wikipedia for a good explanative article), and their evaluation requires a series of formulas along with P and Q which are shown on pages 261-262 of the above Theory of Numbers by Beiler. The formulas are defined in the book and are as follows:

P1 = 0
Q1 = 1
a1 is the integral part of D
P2 = a1
Q2 = D − a12
Pn = a1Qn-1 − Pn-1
Qn = (D − Pn2) ∕Qn-1
an is the integral part of (a1 + Pn) ∕Qn

In addition, for finding the nth convergent pn ∕qn from the previous two convergents and the as, Beiler gives the formulas:

pn = anpn-1 + pn-2
qn = anqn-1 + qn-2
q1 = 1, p1 = a1, q2 = a2, p2 = a2a1 + 1
pn2 − Dqn2 = (−1)nQn+1

This last equation is important since it determines whether n is the negative Pell solution (n is odd) or the regular Pell solution (n is even). In addition, when Qn+1 = 1 the Pell x,y values are located at pn,qn, respectively.

An example which he gives in the book is that of conversion of 23 into a continued fraction. The result using the formulas from above is shown in Table I:

Table I
n 12345678910
Pn0433443344
Qn1727172717
an4131813181
pn45192421123591611511012411275
qn1145444919124021112351

Thus when Q5 and Q9 both equal one then p4 = 24 and q4 = 5, and p8 = 1151 and q8 = 240, respectively, fulfilling the last formula above. As can be seen only the positive Pell solutions are present since n is even.

Calculation of all five values using Computer Code

The calculations for finding the convergents and generating a table of results using JS computer code is the purpose of this article. Note that the file is a txt file and must first be converted to an html file if one wishes to input their own Ds. A caveat, however: Errors sometime crop up when the numbers are large. Also because JS uses 64 bits for a number, very large number results are often depicted in exponential form.

We can generate a computer program that will give the above results with an even greater number of columns. The way the program is set up is as follows:

Computer Programming Examples

The JS computer code simplififies the calculations. For example, D=211 was given as an example on page 259 in the Bailer Book for us to try to solve. It's easy peasy with the program. It's at n=26 one less than Q27 = 1.
As a final humoristic aside the results for D=1021 by this method gives values at n=49 (the − Pell Solution) for p49 and q49 of 3.15217×1023 and 9.86500×1021, respectively. That's very close to the Avogadro number of 6.02×1023.

The values calculated by this program can be checked by searching Google books:Canon Pellianus which contains about 1000 entries. It's an old book which probably required lots of manual, tedious calculations in monklike surroundings using the very top two expressions.

This concludes Part XIII. Go back to Part XIIB.

Go back to homepage.


Copyright © 2021 by Eddie N Gutierrez. E-Mail: enaguti1949@gmail.com