ON SHANKS ALGORITHM FOR COMPUTING THE CONTINUED FRACTION OF log b a. TERENCE JACKSON AND KEITH MATTHEWS Abstract. We give a more practical variant of Shanks 954 algorithm for computing the continued fraction of log b a, for integers a > b >, using the floor and ceiling functions and an integer parameter c >. The variant, when repeated for a few values of c = 0 r, enables one to guess if log b a is rational and to find approximately r partial quotients.. Shanks algorithm In his article [], Shanks gave an algorithm for computing the partial quotients of log b a, where a > b are positive integers greater than. Construct two sequences a 0 = a, a = b, a 2,... and n 0, n, n 2,..., where the a i are positive rationals and the n i are positive integers, by the following rule: If i and a i > a i >, then (.) (.2) a ni i a i < a ni + i a i+ = a i /a ni i. Clearly (.) and (.2) imply a i > a i+. Also (.) implies a i a /ni i i and hence by induction on i 0, (.3) (.4) Also by induction on j 0, a i+ a /n0 ni 0. a 2j = a r 0/a s, a 2j+ = a u /a v 0, where r and u are positive integers and s and v are non negative integers. Two possibilities arise: (i) a r+ = for some r. Then equations (.4) imply a relation a q 0 = ap for positive integers p and q and so log a a 0 = p/q. (ii) a i+ > for all i. In this case the decreasing sequence {a i } tends to a. Also (.3) implies a =, unless perhaps n i = for all sufficiently large i; but then (.2) becomes a i+ = a i /a i and hence a = a/a =. If a i > a i >, then from (.) we have log ai (.5) n i =. log a i Let x i = log ai+ a i if a i+ >. Then we have Lemma. If a i+2 >, then (.6) x i = n i + /x i+. 99 Mathematics Subject Classification. Primary D09. for
2 TERENCE JACKSON AND KEITH MATTHEWS Proof. From (.2), we have (.7) (.8) (.9) from which (.6) follows. log a i+2 = log a i n i log a i+ = log a i log a i+ n i log a i+ log a i+ log a i+2 log a i+2 = x i x i+ n i x i+, From Lemma. and (.5), we deduce Lemma 2. (a) If log a a 0 is irrational, then x i = n i + /x i+ for all i 0. (b) If log a a 0 is rational, with a r+ =, then { ni + /x x i = i+ if 0 i < r, n r if i = r. In view of the equation log a a 0 = x 0, Lemma 2 leads immediately to Corollary. (.0) log a a 0 = { [n0, n,... ] if log a a 0 is irrational, [n 0, n,..., n r ] if log a a 0 is rational and a r+ =. Remark. It is an easy exercise to show that for j 0, (.) a 2j = a q2j 2 0 /a p2j 2, a 2j+ = a p2j a q2j 0 where p k /q k is the k th convergent to log a a 0. Example. log 2 0: Here a 0 = 0, a = 2. Then 2 3 < 0 < 2 4, so n 0 = 3 and a 2 = 0/2 3 =.25. Further,.25 3 < 2 <.25 4, so n = 3 and a 3 = 2/.25 3 =.024. Also,.024 9 <.25 <.024 0, so n 2 = 9 and a 4 =.25/.024 9 = 250000000000000000000000000/23794003928538027489924224 =.009749586 Continuing we obtain Table and log 2 0 = [3, 3, 9, 2, 2, 4, 6, 2,,,... ].
ON SHANKS ALGORITHM FOR COMPUTING THE CONTINUED FRACTION OF log b a. 3 Table i n i a i p i/q i 0 3 0 3/ 3 2 0/3 2 9.25 93/28 3 2.024 96/59 4 2.009749586 485/46 5 4.0043362776 236/643 6 6.00045475 330/4004 7 2.00062894 28738/865 8.0000637223 42039/2655 9.0000354408 70777/2306 0.0000282805.00000760 2. Some Pseudocode In Table 2 we present pseudocode for the Shanks algorithm. It soon becomes impractical to perform the calculations in multiprecision arithmetic, as the numerators and denominators a i grow rapidly. If we truncate the decimal expansions of the a[i] to r places and represent a positive rational a as g(a)/0 r, where g(a) = 0 r a, the ratio aa/bb will be calculated as 0 r g(aa)/g(bb). Working explicitly in integers, using the g(a), then results in algorithm, also depicted in Table 2, with c = 0 r, where int(x,y) equals x/y, when x and y are integers. As shown in the next section, the A[i] decrease strictly until they reach c. Also m[0]=n[0] and we can expect a number of the initial m[i] will be partial quotients. Naturally, the larger we take c, the more partial quotients will be produced. 3. Formal description of algorithm We show in Theorem 2. below, that algorithm will give the correct partial quotients when log a a 0 is rational and otherwise gives a parameterised sequence of integers which tend to the correct partial quotients when log a a 0 is irrational. Algorithm is now explicitly described. We define two integer sequences {A i,c }, i = 0,..., l(c) and {m j,c }, j = 0,..., l(c) 2, as follows. Let A 0,c = c a 0, A,c = c a. Then if i and A i,c > A i,c > c, we define m i,c and A i+,c by means of an intermediate sequence {B i,r,c }, defined for r 0, by B i,0,c = A i,c and cbi,r,c (3.) B i,r+,c =, r 0. A i,c Then c B i,r+,c < B i,r,c, if B i,r,c A i,c > c and hence there is a unique integer m = m i,c such that B i,m,c < A i,c B i,m,c. Then we define A i+,c = B i,m,c. Hence A i+,c c and the sequence {A i,c } decreases strictly until A l(c),c = c. There are two possible outcomes, depending on whether or not log b (a) is rational:
4 TERENCE JACKSON AND KEITH MATTHEWS Table 2 Shanks algorithm algorithm input: integers a>b> input: integers a>b>, c> output: n[0],n[],... output: m[0],m[],... s:= 0 s:= 0 a[0]:= a; a[]:= b A[0]:= a*c; A[]:= b*c aa:= a[0]; bb:= a[] aa:= A[0]; bb:= A[] while(bb > ){ while(bb > c){ i:=0 i:=0 while(aa bb){ while(aa bb){ aa:= aa/bb aa:= int(aa*c,bb) i:= i+ i:= i+ } } a[s+2]:= aa A[s+2]:= aa n[s]:= i m[s]:= i t:= bb t:= bb bb:= aa bb:= aa aa:= t aa:= t s:= s+ s:= s+ } } Theorem 2. () log a a 0 is a rational number p/q, p > q, gcd(p, q) =. Then (a) a 0 = d p, a = d q for some positive integer d; (b) if p/q = [n 0,..., n r ], where n r > if r >, then (i) A r+,c = c, a r+ = ; (ii) A i,c = c a i for 0 i r + ; (iii) m i,c = n i for 0 i r. (2) log a a 0 is irrational. Then (a) m 0,c = n 0 ; (b) l(c) and for fixed i, A i,c /c a i as c and m i,c = n i for all large c. Proof. (a) follows from the equation a p = aq 0. (b) is also straightforward on noticing that a i is a power of d and that we are implicitly performing Euclid s algorithm on the pair (p, q). For 2(a), we have (3.2) a n0 < a 0 < a n0+ and A 0,c = c a 0, A,c = c a. Also by induction on 0 r n 0, (3.3) B,r,c ca n0 r, B,r,c ca 0 (3.4) a r. Inequality (3.3) with r n 0 gives B,r,c A,c, while inequality (3.4) with r = n 0 gives B,n0,c ca 0 a n0 < ca = A,c,
ON SHANKS ALGORITHM FOR COMPUTING THE CONTINUED FRACTION OF log b a. 5 by inequality (3.2). Hence m 0,c = n 0. For 2(b), we use induction on i and assume l(c) i holds for all large c and that A i,c /c a i and A i,c /c a i as c. This is clearly true when i =. By properties of the integer part symbol, equation (3.) gives (3.5) c r A i,c A r i,c ( cr A r i,c ) c A i,c for r 0. Hence for r < n i, inequalities (3.5) give < B i,r,c cr A i,c A r. i,c B i,r,c /c a i /a r i a i /a ni i > a i. Then, because A i,c /c a i, it follows that B i,r,c > A i,c for all large c. Also B i,ni,c/c a i /a ni i < a i, so B i,ni,c < A i,c for all large c. Hence m i,c = n i for all large c. Also A i+,c = B i,ni,c > c, so l(c) > i+ for all large c. Moreover A i+,c /c a i /a ni i = a i+ and the induction goes through. Example 3. Table 3 lists the sequences m 0,c,..., m l(c) 2,c for c = 2 u, u =,..., 30, when a 0 = 3, a = 2. Table 3,,,,,,,,,,,,2,,,,2,,,,2,3,,,,2,2,2,,,,2,2,2,,,,,2,2,2,,2,,,,2,2,3,2,3,,,,2,2,3,2,,,,2,2,3,,2,,,, 2,,,,2,2,3,,3,,,3,,,,,2,2,3,,4, 3,,,,,2,2,3,,4,, 9,,,,,2,2,3,,5,24,,2,,,,2,2,3,,5, 3,,, 2,7,,,,2,2,3,,5, 2,,, 5,3,,,,,2,2,3,,5, 2, 2,, 3,,6,,,,2,2,3,,5, 2,5,, 6,2,,,2,2,3,,5, 2, 9,5,,2,,,,2,2,3,,5, 2,3,,,, 6,, 2, 2,,,,2,2,3,,5, 2,7,2, 7,8,,,,2,2,3,,5, 2,9,,49,2,,,,,2,2,3,,5, 2,22,4, 8,3, 4,,,,,2,2,3,,5, 2,22,2,,3,, 3, 8,,,,2,2,3,,5, 2,22,, 6,3,,, 3, 4, 2,,,,2,2,3,,5, 2,23,2,,, 2,,2,7,,,,2,2,3,,5, 2,23,3, 2,2, 2, 2,, 3, 2,,,,2,2,3,,5, 2,23,2,,7, 2, 2,4,,, 6, In fact log 2 3 = [,,, 2, 2, 3,, 5, 2, 23, 2,... ].
6 TERENCE JACKSON AND KEITH MATTHEWS 4. A heuristic algorithm We can replace the x function in equation (3.) by x, the least integer exceeding x. This produces an algorithm with similar properties to algorithm, with integer sequences {A i,c }, i = 0,..., l (c) and {m j,c }, j = 0,..., l (c) 2. Here A 0,c = A 0,c = a 0 c, A,c = A,c = a c and m 0,c = m 0,c = n 0. Then if i and A i,c > A i,c > c, we define m i,c and A i+,c by means of an intermediate sequence {B i,r,c }, defined for r 0, by B i,0,c = A i,c and (4.) and B i,r+,c = cb i,r,c A i,c Then c B i,r+,c < B i,r,c, if B i,r,c A i,c > c. For cb i,r,c A i,c B i,r+,c cb i,r,c A i,c, r 0. + + B i,r,c cb i,r,c + A i,c A i,cb i,r,c A i,c A i,c c B i,r,c. The last inequality is certainly true if B i,r,c A i,c > c. Hence there is a unique integer m = m i,c such that B i,m,c < A i,c B i,m,c. Then we define A i+,c = B i,m,c. Hence A i+,c c and the sequence {A i,c } decreases strictly until A l (c),c = c. If we perform the two computations simultaneously, the common initial elements of the sequences {m j,c } and {m k,c } are likely to be partial quotients of log b(a). With c = 0 r we expect roughly r partial quotients to be produced. If l(c) = l (c) and A j,c = A j,c and m j,c = m j,c for j = 0,..., l(c) 2, then log b a is likely to be rational. In practice, to get a feeling of certainty regarding the output when c = 0 r, we also run the algorithm for c = 0 t, r 5 t r + 5. Example 4. Table 4 lists the common values of m i,c and m i,c, when a = 3, b = 2 and c = 2 r, r 3. It seems likely that only partial quotients are produced for all r. Example 5. Table 5 lists the common values of m i,c and m i,c, when a = 34, b = 2 and c = 0 r, r 20. Partial quotients are not always produced, as is seen from lines 9,4 and 7. 5. Acknowledgement The second author is grateful for the hospitality provided by the School of Mathematical Sciences, ANU, where research for part of this paper was carried out.
ON SHANKS ALGORITHM FOR COMPUTING THE CONTINUED FRACTION OF log b a. 7 Table 4. a = 3, b = 2, c = 2 r, r 3. : 2: 3:,, 4:,, 5:,,,2 6:,,,2 7:,,,2,2 8:,,,2,2 9:,,,2,2 0:,,,2,2 :,,,2,2 2:,,,2,2 3:,,,2,2,3, 4:,,,2,2,3, 5:,,,2,2,3, 6:,,,2,2,3,,5 7:,,,2,2,3,,5 8:,,,2,2,3,,5 9:,,,2,2,3,,5,2 20:,,,2,2,3,,5 2:,,,2,2,3,,5,2 22:,,,2,2,3,,5,2 23:,,,2,2,3,,5,2 24:,,,2,2,3,,5,2 25:,,,2,2,3,,5,2 26:,,,2,2,3,,5,2 27:,,,2,2,3,,5,2 28:,,,2,2,3,,5,2,23 29:,,,2,2,3,,5,2,23 30:,,,2,2,3,,5,2,23,2 3:,,,2,2,3,,5,2,23,2 Table 5. a = 34, b = 2, c = 0 r, r =,..., 20. :,2,2 2:,2,2,, 3:,2,2,,,2 4:,2,2,,,2 5:,2,2,,,2,3, 6:,2,2,,,2,3,,8, 7:,2,2,,,2,3,,8,, 8:,2,2,,,2,3,,8,,,2 9:,2,2,,,2,3,,8,,,2,2,,3,3,2,32,7 0:,2,2,,,2,3,,8,,,2,2, :,2,2,,,2,3,,8,,,2,2,,2, 2:,2,2,,,2,3,,8,,,2,2,,2, 3:,2,2,,,2,3,,8,,,2,2,,2,,3 4:,2,2,,,2,3,,8,,,2,2,,2,,3,3,3 5:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2 6:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2,2 7:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2,2,8,,,,, 8:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2,2,7, 9:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2,2,7, 20:,2,2,,,2,3,,8,,,2,2,,2,,3,3,2,2,7, References. D. Shanks, A logarithm algorithm, Math. Tables and Other Aids to Computation 8 (954). 60 64. DEPARTMENT OF MATHEMATICS, UNIVERSITY OF YORK, HESLINGTON. YORK YO05DD, ENGLAND E-mail address: thj@york.ac.uk DEPARTMENT OF MATHEMATICS, UNIVERSITY OF QUEENSLAND, BRISBANE, AUSTRALIA, 4072 E-mail address: krm@maths.uq.edu.au