mariusjp
mariusjp
February 5, 2003
Abstract
This paper seeks to recapitulate the known facts
√ about the length of
the period of the continued fraction expansion of n as a function of n
and to make a few (possibly) original contributions. I have
√ established
a result concerning the average period length for k < n < k + 1,
where k is an integer, and, following numerical experiments, tried to
formulate the best possible bounds for this average length and for the
maximum
√ length
√ of the period of the continued fraction expansion of
n, with b nc = k.
Many results used in the course of this paper are borrowed from [1] and
[2].
1 Preliminaries
Here are some basic definitions and results that will prove useful throughout
the paper. They can also be probably found in any number theory intro-
ductory course, but I decided to include them for the sake of completeness.
P0 = a0 , Q0 = 1,
P1 = a0 a1 + 1, Q1 = a1 ,
1
√
2 Period of the Continued Fraction of n
and in general
1
Rn = a0 +
1
a1 +
1
a2 +
.. 1
.+
an .
The proof can be easily made by induction, and is also to be found in [2,
Sect. 5.2.4]. Other well-known facts are that
If not much can be said in general about the period of a quadratic irra-
tional x (after all, every periodic sequence of integers determines one such
irrational), a lot is √
known about the continuous fraction expansion of irra-
tionals of the form D, for rational D = pq .
Marius Beceanu 5
√
Theorem 2.3 In the continued √ fraction expansion of D, the remainders
(D)+bn
always take the form xn = cn , where the numbers bn , cn , as well as
the continued fraction digits an can be obtained by means of the following
algorithm: set a0 = bDc, b1 = a0 , c1 = D − a2o , and then compute
D − b2n
a0 + bn−1
an−1 = , bn = an−1 cn−1 − bn−1 , cn = .
cn−1 cn−1
√ Pn−1 xn +Pn−2
Proof We already know that D= Qn−1 xn +Qn−2 , or equivalently
√ √ √
−Qn−2 D + Pn−2 (Qn−2 D − Pn−2 )(Qn−1 D + Pn−1 )
xn = √ = 2
Qn−1 D − Pn−1 Pn−1 − DQ2n−1
√
(−1)n−1 D + DQn−1 Qn−2 − Pn−1 Pn−2
= 2 .
Pn−1 − DQ2n−1
Then, we know precisely the values of bn and cn (which, in case they exist,
must be unique, being rational), namely
The claims concerning the recurrence relation can be verified direcly. First,
though, we need the following:
Its proof lies in [1, pp. 295-296]. By using this lemma, one can easily find
that √ √
D + bn b D + bn c a0 + bn
an = bxn c = = = ,
cn cn cn
while a simple computation shows that
√ √
D + bn−1 1 cn an−1 D + an−1 bn + cn
= xn−1 = an−1 + = an−1 + √ = √
cn−1 xn D + bn D + bn
and equivalently
√ √ √
( D + bn−1 )( D + bn ) = cn−1 (an−1 D + an−1 bn + cn )
√
6 Period of the Continued Fraction of n
√
whence we get (since D is irrational) that
and
√ √ √
denominator, D+b cn
n
> 1 implies D + b n > cn , and 0 < D−bn
cn < 1
√
implies
√ D− √ bn < cn . Finally, combining these two inequalities we get
D − bn < D + bn , so bn > 0. Thus, we have proved, albeit in a different
order, all the promised inequalities, q. e. d.
An immediate consequence is
√
Corollary 2.1 The continued fraction expansion of D is periodic, with a
period of at most pq, if D = pq .
b̃n
Indeed, in virtue of their representation (2.1), we can write bn = q and
c̃n
cn = q , where b̃n and c̃n are integers. For them, the following relations
√ √ √
hold: 0 < b̃n < pq and pq − b̃n < c̃n < pq + b̃n . Thus, c̃n can take
at most 2b̃n − 1 value for each b̃n , and the number of possible distinct pairs
(b̃n , c̃n ) is no greater than
√
b pqc
X √
2b̃n − 1 = b pqc2 < pq.
b̃n =1
1 1
= 2a0 +
x01 1
a1 +
1
a2 +
.. 1
. ap−1 + xn
1 1 1 1
0 = 0 = ap + x0p = ap + = ap +
x1 xp+1 1 ap−1 + x0p−1
x0p
1 1
= ap + .
x01 1
ap−1 +
1
ap−2 +
..
. a1 + x01
Since x01 < 1, both are portions of the continued fraction expansion of x,11 ,
and therefore they must coincide. By identifying the coefficients we obtain
that 2a0 = ap , a1 = ap−1 , · · · , ak = ap−k , q. e. d.
3 Numerical experiments
I tried to find out the relation between n and the size of the period in the
√
continued fraction expansion of n, in brief l(n). I used for this purpose
the algorithm described in Theorem 2.3, implemented in C, in the program
attached to this paper. I tried to evaluate both the average size of l(n) and
its maximal values.
Table 1 contains the successive peaks of l(n)
√ , for 1 < n < 108 , that is to
n
say the values n for which l(n)
√ > √
n
l(m)
m
for all nonsquare m < n. Apparently,
the ratio gets ever larger, by increasingly smaller increments. However, the
table does not make it clear whether the ratio is bounded from above. One
Marius Beceanu 9
Pb√nc
conclusion that can be drawn from it, though, is that neither k=1 d(n −
√
k 2 ), nor n log n are good upper bounds for l(n); both are much too large.
The graph below tries to bring an intuitive answer to the question
whether the quantity l(n)
√ is bounded, by considering the peaks of this func-
n
tion as given in Table 1 (and disregarding the precise points where they are
attained).
l(n)
Figure 1: The peaks of √ ,
n
plotted in successive order, for 1 < n < 108 .
Here, the upper line represents the peaks, and the lower one the differ-
ences between consecutive peaks; the x-coordinates of the points are equally
spaced. This picture seems to indicate that the ratio is not bounded.
The subsequent graph (Figure 2) illustrates the evolution of l(n) versus
√ √
n, for 1 < n < 1001. The upper line represents local maxima, while
the lower line represents averages of l(n), both computed on intervals of
√ √
length 1 (k < n < k + 1, k ∈ N). The red line represents k = b nc for
√
comparison. It is clear that l(n) and n have the same order of magnitude,
on the average.
The next diagram (Figure 3) represents the ratio bl(n)
√ , both in average
nc
√
and the maximum value for b nc = k, as a function of k. The upper line
represents local maxima and the lower line averages of bl(n)
√ , both taken on
nc
√
the intervals k < n < k + 1. The red line represents y = 1 for comparison.
√
It can be clearly seen that the average of bl(n)
√
nc
for b nc = k is less than k,
while the maximum of this function on the same interval is bigger than k.
Finally, the last graph (4) represents the maximum value of the ra-
√
10 Period of the Continued Fraction of n
√ √
Figure 2: l(n) plotted against b nc, 1 < n < 1001.
l(n) √ √
Figure 3: √
b nc
plotted versus b nc, 1 < n < 1001.
Marius Beceanu 11
tio, namely maxb√nc=k l(n) k , and the inverse of the ratio’s average value,
k
l(k2 +1)+...+l(k2 +2k)
, versus log log k.
2k
l(n) 2k2
Figure 4: maxb√nc=k k and l(k2 +1)+...+l(k2 +2k)
plotted versus log log k, 3 ≤
k ≤ 10000.
and
Pk2 +2k
k n=k2 +1 l(n) k
≤ ≤ .
a2 log log k + b2 2k a2 log log k + c2
√
12 Period of the Continued Fraction of n
√ l(n) Pb√nc √
n l(n) n √
n k=1 d(n − k2 ) n log n
2 1 1.414 0.707 0 1.0
3 2 1.732 1.155 0 1.9
7 4 2.646 1.512 4 5.1
43 10 6.557 1.525 26 24.7
46 12 6.782 1.769 28 26.0
211 26 14.526 1.790 98 77.7
331 34 18.193 1.869 140 105.6
631 48 25.120 1.911 218 162.0
919 60 30.315 1.979 278 206.8
1726 88 41.545 2.118 418 309.7
4846 152 69.613 2.183 820 590.7
7606 194 87.212 2.224 1130 779.4
10399 228 101.975 2.236 1368 943.2
10651 234 103.204 2.267 1438 957.0
10774 238 103.798 2.293 1420 963.8
18379 322 135.569 2.375 2034 1331.1
19231 332 138.676 2.394 2052 1367.9
32971 438 181.579 2.412 2916 1889.0
48799 544 220.905 2.463 3720 2384.8
61051 614 247.085 2.485 4278 2722.7
78439 696 280.070 2.485 5048 3156.4
82471 716 287.178 2.493 5124 3250.9
111094 834 333.308 2.502 6188 3872.4
162094 1016 402.609 2.524 7720 4829.7
187366 1106 432.858 2.555 8460 5255.3
241894 1262 491.827 2.566 9916 6096.8
257371 1318 507.317 2.598 10340 6320.3
289111 1400 537.690 2.604 10964 6761.2
294694 1438 542.857 2.649 11308 6836.6
799621 2383 894.215 2.665 25834 12154.1
969406 2664 984.584 2.706 22740 13571.9
1234531 3030 1111.095 2.727 26544 15584.4
1365079 3196 1168.366 2.735 28240 16505.2
1427911 3308 1194.952 2.768 29026 16934.5
1957099 3898 1398.964 2.786 34808 20266.7
2237134 4212 1495.705 2.816 38004 21868.3
2847079 4784 1687.329 2.835 43964 25076.8
5715319 6892 2390.673 2.883 66394 37195.7
10393111 9352 3223.835 2.901 93342 52086.4
12843814 10442 3583.827 2.914 105148 58661.4
14841766 11226 3852.501 2.914 114260 63616.2
18461899 12542 4296.731 2.919 129960 71889.6
20289091 13358 4504.341 2.966 138224 75788.2
23345326 14348 4831.700 2.970 149824 81974.2
28473454 15876 5336.052 2.975 167880 91590.6
39803611 19002 6309.010 3.012 204556 110404.3
40781911 19396 6386.072 3.037 208528 111907.9
4 Theoretical results
Though by far worse that the bounds which seem attainable in practice,
these are my results concerning l(n).
Let d(n) denote, in the sequel, the number of positive (integer) divisors
of n.
Theorem 4.1 For every > 0, there exists C() such that
√ (1+) log D
l(n) < C() + D2 log log D .
√
Here a bound of the form l(n) = O( n(A + B log log n) is probably attain-
able.
Proof For each pair bn , cn , we have cn cn−1 = D − b2n , or cn |D − b2n . Thus,
the number of possible pairs cannot exceed
√
b Dc
X
d(D − b2 ).
b=1
log n
(1+)
By [3, Theorem 317], for any > 0, d(n) < 2 log log n for all sufficiently
large n (n > n0 ()). Then, since the function that bounds d(n) in the
inequality above is increasing, it follows that
√
b Dc
X
l(D) ≤ d(D − b2 )
b=1
X X
≤ d(D − b2 ) + d(D − b2 )
√ √
1≤b≤b Dc 1≤b≤b Dc
D−b2 ≤n0 () D−b2 >n0 ()
n0 ()
log D−b2
X X (1+)
≤ d(k) + 2 log log D−b2
√
k=1 1≤b≤b Dc
2
D−b >n0 ()
log D
(1+) log
X
≤ C() + 2 log D
√
1≤b≤b Dc
2
D−b >n0 ()
√ log D
(1+) log
< C() + D·2 log D .
√
By using the result in [4], namely that 1≤k<√n d(n−k 2 ) = O( n log3 n),
P
it is possible to improve this result to
√
l(n) = O( n log3 n).
√
14 Period of the Continued Fraction of n
Concerning the next theorem, the best possible bound is probably better
k
than the one presented below, probably in the order of a2 +c2 log log k for the
average of l(D) on the interval [k 2 + 1, k 2 + 2k]. Nevertheless, I have not
managed to find the proof for a better bound than the one below.
Theorem 4.2 The average size of l(D) for k 2 < D < (k + 1)2 is no greater
than 74 k + 34 .
√
D+bn
Proof In order that xn = cn , where xn is a remainder in the continuous
√ .
2 ..c and
√
fraction
√ decomposition of D, we need to have D − b n n D > bn >
| D − cn | (by Theorem 2.4).
For fixed bn and cn , the number of D such that cn | D −b2n cannot exceed
2k 2
cn + 1, because D − bn takes values in an interval of 2k integers. Then, let
us distinguish two cases: cn ≤ k and cn > k.
In the first case, we have k − cn < bn ≤ k, so the number of possible values
for bn is cn .
In the second case, we have cn − k < bn ≤ k, so the number of possible bn
cannot exceed 2k − cn . Furthermore, since cn > k, the number of multiples
of cn in each interval k 2 +1−b2n , . . . , k 2 +2k −b2n (otherwise said, the number
of D with k 2 < D < (k + 1)2 and cn | D − b2n ) cannot exceed 2.
Marius Beceanu 15
Appendix
This is the program that I wrote in order to evaluate the size of l(D).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int period_length(mint n) {
int a_0, a, b, c, b_0, c_0, result=0;
a_0=sqrt(n*1.0);
b=b_0=a_0;
c=c_0=n-a_0*a_0;
do {
a=(a_0+b)/c;
b=a*c-b;
c=(n-b*b)/c;
result++;
} while ((b!=b_0)||(c!=c_0));
return result;
}
int nodiv(int n) {
int i, j, result=1;
for (i=2; i*i<=n; i++)
if ((n%i)==0) {
j=1;
√
16 Period of the Continued Fraction of n
while ((n%i)==0) {
j++;
n=n/i;
}
result=result*j;
}
if (n!=1) return result<<1;
return result;
}
int estim(int n) {
int i, result=0, j=sqrt(n*1.0);
for (i=1; i<j; i++)
result+=nodiv(n-i*i);
return result;
}
int main() {
int i, j, i2, imax;
int l, lmax;
double r, r0=0.0, s, lavg;
int beginning_number=9332, end_number=10001;
printf("%d %d\n", beginning_number, end_number);
for (i=beginning_number*beginning_number,
i2=beginning_number, imax=i+(i2<<1)+1;
i2<end_number;
i=imax, i2++, imax+=(i2<<1)+1) {
lavg=0.0;
lmax=0;
for (j=i+1; j<imax; j++) {
l=period_length(j);
if (l>lmax) lmax=l;
s=sqrt(j*1.0);
r=l/s;
lavg+=l*1.0;
if (r>r0) {
r0=r;
fprintf(stderr,
"n=%d l(n)=%d sqrt(n)=%.3f r=%.3f "
"est=%d 2nd est=%.1f\n",
Marius Beceanu 17
j, l, s, r, estim(j), sqrt(j*1.0)*log(j*1.0));
}
}
printf("Square root= %d Maximum= %d Average= %.3f\n",
i2, lmax, lavg/((imax-i-1)*1.0));
}
return 0;
}
√
In order to compute l(n) for n between 1 and k, the program takes
2 −1
kX
O l(n)
n=2
time. According to my best estimates, this does not exceed O(k 3 ), but is
3
probably even lower (probably O logklog k ). The program took almost a day
√
(23 hours) to run for 1 < n < 104 .
Different C programs were also employed to produce the diagrams in-
cluded in the paper.
References
[1] W. Sierpinski, Elementary Theory of Numbers, Panstwowe
Wydawnictwo Naukowe, Warszawa, 1964.
[5] J. H. E. Cohn, The Length of the Period of the Simple Continued Frac-
tion of d1/2 , Pacific Journal of Mathematics, Vol. 71, No. 1, 1977, pp.
21-32.
√
18 Period of the Continued Fraction of n