Professional Documents
Culture Documents
by
Bill Gosper
Abstract: Contrary to everybody, this self contained paper will show that
continued fractions are not only perfectly amenable to arithmetic, they are
amenable to perfect arithmetic.
Output
1
= 2 + ---------
1
1 + ---------
1
1 + ---------
1
5 + --------
1
1 + ---
3
Similarly, if you want 100/2.54, the number of inches per meter,
you will find
39 2 1 2 2 1 4
39.(370078740157480314960629921259842519685039)
where the part in parentheses repeats forever. (Incidentally, this
repeating decimal is easily computed since the remainder of 2 after
the quotient digits 3937 ensures that, starting with 7874..., the
answer will be just twice the original one, ignoring the
decimal point. Thus you just double the quotient, starting from the
left (using one digit lookahead for carries), placing the answer
digits on the right, so as to make the number chase its tail. This
trick is even easier in expansions of ratios where some remainder
is exactly 1/nth of an earlier one, for small n. You forget about
the divisor and simply start shortdividing by n at the quotient
digit corresponding to the earlier remainder. If this seems confusing,
forget it--it has nothing to do with continued fractions.)
70 t + 29
--------- ,
12 t + 5
70 70 t + 29 29
-- < --------- < --
12 12 t + 5 5
70 t + 29
12 t + 5 5 ( 70 t + 29 - 5 (12 t + 5) = 10 t + 4 )
2 t
All we can say about our next quotient is that it lies between
1 and oo. But we have managed to reexpress
70 t + 29 1
--------- as 5 + ---------
12 t + 5 1
1 + ---------
1
4 + ---------
2 t + 1
-------
2 t
and thereby determine the first three terms of the continued fraction.
Input
Now, the opposite problem: suppose you are receiving the terms
of a continued fraction 5 1 4 1 ... which may go on forever, or
possibly end with a symbolic expression. We wish to reconstruct
the value as the terms come in, rather than awaiting an end which
may never come.
1 5 x + 1
x becomes 5 + - or ------- .
x x
1 5 x + 1 6 x + 5
x becomes 1 + - and ------- becomes ------- .
x x x + 1
Then
1 6 x + 5 29 x + 6
x becomes 4 + - and ------- becomes -------- .
x x + 1 5 x + 1
Then
1 29 x + 6 35 x + 29
x becomes 1 + - and -------- becomes --------- .
x 5 x + 1 6 x + 5
In general, when
1 q x + r (a q + r) x + q
x becomes a + - , then ------- becomes --------------- .
x s x + t (a s + t) x + s
From RIGHT TO LEFT across the page, we write the incoming terms as
we learn them:
. . . 1 4 1 5
Under the first (rightmost) term, we place the left edge of the array
1 0 1 x + 0
symbolizing ------- i.e. the identity function:
0 1 0 x + 1
. . . 1 4 1 5
1 0
0 1
Then, again from RIGHT TO LEFT, we extend the lower two rows with the
simple recurrence: multiply each element by the term in the top row
above it, then add the element to the right and write the sum on the
left:
. . . 1 4 1 5
35 29 6 5 1 0
6 5 1 1 0 1
2t 1 4 1 5
70t + 29 35 29 6 5 1 0
12t + 5 6 5 1 1 0 1
The great thing about this process is that you can take other
functions by initializing the rightmost matrix to other than
1 0
0 1 .
Throughput
2
for -----------
3 - sqrt(2)
0 2
-1 3
symbolizing
0 sqrt(2) + 2
------------- .
- sqrt(2) + 3
. . . 2 2 2 2 2 2 1
4 2 0 2
3 2 -1 3
4 2 4 x + 2
But means ------- and since x, the rest of the continued
3 2 3 x + 2
. . . 2 2 2 2 2 2 1
4 2 0 2
3 2 -1 3 1
1 0
(As before, we list the output terms down the right side.)
Now we must proceed to the left again (unless we are willing to admit
that we know x > 2):
. . . 2 2 2 2 2 2 1
4 2 0 2
8 3 2 -1 3 1
2 1 0
Any number between 8/2 and 3/1 has 3 as its integer part,
so 3 is our second term.
. . . 2 2 2 2 2 2 1
4 2 0 2
8 3 2 -1 3 1
2 1 0 3
2 0
Continuing:
. . . 2 2 2 2 2 2 1
4 2 0 2
8 3 2 -1 3 1
5 2 1 0 3
10 4 2 0 1
5 2 1 0 4
10 4 2 0 1
2 1 0 4
But we have been in a loop since the second occurrence of the pattern
2 1
2 0
2
----------- is 1 3 1 4 1 4 1 4 . . . .
3 - sqrt(2)
1 e - 1
tanh - = -----
2 e + 1
. . . 1 1 4 1 1 2 1 2
1 1 -1
4 3 1 1 0
12 7 5 2 1 1 2
20 11 9 2 1 1 0 1 6
2 1 1 0 1 10
0 1
2 1
0 1 ,
. . . 1 1 2k+6 1 1 4 1 1 2 1 2
1 1 -1
4 3 1 1 0
12 7 5 2 1 1 2
20 11 9 2 1 1 0 1 6
2 1 1 0 1 4k+14
0 1
2 1 e - 1
proves that ----- = 0 2 6 10 (4k+14) = 0 (4k+2) .
0 1 e + 1
if x = (f(k) 1 1)
then
2 x + 1
------- = 2 x + 1 = (2f(k)+2) .
0 x + 1
(8k+12)*1 - (4k+7)*2 = -2
1 * 1 - (-1) * 1 = 2
p x + q
------- ,
r x + s
p q
r s
1
x = a b c ... = a + ---------
1
b + ---------
1
c + ---------
...
a 1 b 1 c 1
( ) ( ) ( ) . . .
1 0 1 0 1 0
p q a 1 b 1 c 1
( ) ( ) ( ) ( ) . . .
r s 1 0 1 0 1 0
0 1 2 1 f 1 1 1 1 1 2 1
( ) ( ) ( ) ( ) ( ) = ( ) .
1 -2f-2 0 1 1 0 1 0 1 0 0 1
0 4 1 1 -1 2 -2 4 -4
( ) ( ) = ==
1 0 1 -1 1/2 1/2 1 1
The left hand matrix says 4/x, the next one says
x + 1
----- .
x - 1
Inverting it says solve for x (in our case e). (If function
composition comes from matrix multiplication, then function
inversion must come from matrix inversion.) Multiplying by
the first matrix applies the "4 divided by" function. Multiplying
all of the elements by 2 is just equivalent to multiplying the
numerator and denominator of a fraction.
8 0
3 1
by using the fact that x > 1, instead of the weaker condition x > 0,
so that we have
8 8 x + 0 8+0 8 8 x + 0 0
- > ------- > --- instead of - > ------- > - .
3 3 x + 1 3+1 3 3 x + 1 1
. . . 8k+26 8k+22 18 14 10 6 2
4 4 -4
(Determinant = +or- 8)
19 3 1 1 1
91 9 1 3 2
11 1 1 8
-------
43 | 3 1 | 3
| |
26 | 2 -2 | 1
-------
17 1 1
9 1 1
144 8 0 1
19 1 1 7
11 1 1
8 0 1
--------
24k+67 | 3 1 | 2 (Pattern recurs,
| | prompts input of
16k+42 | 2 -2 | period begins 1 symbolic terms.)
--------
8k+25 1 1
8k+17 1 1
64k+208 8 0 k+2
8k+27 1 1 7
8k+19 1 1
8 0 k+2
------------
| 3 1 | Pattern recurs, period ends 2
| |
| 2 -2 |
------------
This gives us
4
- = 1 2 8 3 1 1 1 1 7 1 1 2 (1 1 1 k+2 7 1 k+2 2)
e
= 1 2 8 3 (1 1 1 k+1 7 1 k+1 2) .
The reason for introducing the input term 8k+22 instead of 4k+22 is
that the matrix recurred only every other input term, thus apparently
regarding the input sequence to be (8k+2, 8k+6) instead of simply
(4k+2). This is evidently related to the fact that this process
is characterized by a determinant of +or- 8. A fun conjecture to
test would be the following generalization of Hurwitz's theorem: The
homographic matrix is periodic iff the input sequence is periodic
modulo the determinant.
a x + b -d y + b
y = ------- -> x = -------- .
c x + d c y - a
x = x1 x2 x3 ...
y = y1 y2 y3 ...
then we represent
axy + bx + cy + d
by
. . . x3 x2 x1
b d
a c y1
y2
.
.
.
Floating below the surface of the page, directly beneath the bdac
matrix, we can imagine the denominator matrix
f h
e g
representing
exy + fx + gy + h .
axy + bx + cy + d
-----------------
exy + fx + gy + h
b d
f h
a c
e g .
1 0 1 0
x + y = 0 1 x - y = 0 1
0 1 0 -1
0 0 0 0
0 0 1 0
x * y = 0 1 x / y = 0 0
1 0 0 0
0 0 0 1
y = sqrt(6) = 2 2 4 2 4 2 4 . . .
2
e + 1
x = coth 1 = ------ = 1 3 5 7 9 . . .
2
e - 1
we will compute
2xy + x -2 sqrt(6)
------- = (1 + e ) (1 + -------)
xy + y 12
. . . 5 3 1 <- x
y
1 0 |
0 0
2 0 2
1 1
2
axy + bx + cy + d
-----------------
exy + fx + gy + h
varies between limits among the ratios a/e, b/f, c/g, and d/h,
provided that the denominator doesn't change sign. For the matrix
in question, two of these denominators are zero, and they must be
shifted out of the picture by inputing a term of y.
. . . 5 3 1
1 0
0 0
2 0 2
1 1
5 0 2
2 2
pair of ratios, 2/1 and 5/2, have the same integer parts, whereas the
bottom pair, 5/2 and 0/2, do not. Since our goal is to
get them to be equal so that we can perform a Euclid output step,
we proceed to the left with an x input. Inputing from y instead
would not get rid of the 5/2 and 0/2 for another step.
. . . 5 3 1
1 0
0 0
2 2 0 2
2 1 1
5 5 0 2
4 2 2
. . . 5 3 1
1 0
0 0
8 2 2 0 2
7 2 1 1
20 5 5 0 2
14 4 2 2
7 2
subtract 1 times the denominator matrix from the numerator
14 4
8 2 1 0
matrix to get the new denominator matrix .
20 5 6 1
. . . 5 3 1
outputs
1 0
0 0 1
8 2 2 0 2
7 2 1 1
1 0
20 5 5 0 2
14 4 2 2
6 1
8 2 2 0 2
7 2 1 1
1 0
20 5 5 0 2
14 4 2 2
6 1
35 10 4
13 2
Now 14/6 and 35/13 have equal integer parts, so we move x-ward.
. . . 5 3 1
outputs
1 0
0 0 1
8 2 2 0 2
7 2 1 1
1 0
20 5 5 0 2
74 14 4 2 2
31 6 1
185 35 10 4
67 13 2
. . . 5 3 1
outputs
1 0
0 0 1
2
8 2 2 0 2
7 2 1 1
1 0
20 5 5 0 2
74 14 4 2 2
31 6 1
12 2
185 35 10 4
67 13 2
51 9
Now we must input the 4 from the y sequence, whereupon we will get an
output of 1, leaving us with the matrix 51 9
16 4
216 38
83 20 .
. . . 7 5 3 1
outputs
1 0
0 0 1
2
8 2 2 0 2 1
7 2 1 1 2
1 0 1
1
20 5 5 0 2 .
74 14 4 2 2 .
31 6 1 .
12 2
185 35 10 4
67 13 2
366 51 9
116 16 4
299 58 2
1550 216 38
601 83 20
348 50 .
253 33
95 17 .
3466 483 .
1318 182
830 119
488 63
342 56
Why use algorithms that are at least twice as hard as the usual
algorithms on numbers in positional notation (e.g. decimal or
binary)? One answer is that many numbers, such as pi and e, can be
represented exactly, using little programs (coroutines) to provide
indefinitely many continued fraction terms on demand.
But the algorithms for sum, product, etc. of two such numbers have
this same property, for they produce their output as they read their
input. Thus we can cascade several of these routines so as to
evaluate arithmetic expressions in such a way that output stream
begins almost immediately, and yet can continue almost indefinitely.
The user is freed from having to decide in advance just how much
precision is necessary and yet not wasteful. Later we will extend this
claim to cover series terms and approximating iterations.
PV
-- = R = 8.31432 +or- .00034 Joules/deg/mole
nT
8.31398 + .00068
1.00000 0 8
.31398 + .00068 3
.05806 - .00184 5
.02368 + .00988 2
.01070 - .02160 2
.00328 + .05308
In the fifth row a negative error has greater magnitude than the
corresponding remainder, indicating that we would be outputing
different terms by then if we were doing the upper limit instead. But
we can switch over to doing the upper limit simply by adding the last
two errors to their corresponding remainders and then continuing:
.01070 - .02160 = -.01090
.00328 + .05308 = .05636 0
-.01090 -6
-.00904
8 3 5 2 3
and 8 3 5 2 2 0 -6 = 8 3 5 2 -4 = 8 3 5 1 1 3
= 8 3 5 2 3 0 -7
y = x/y .
ax+b a b
cx-a c -a x
2
b+2ax-cx a-cx
a y + b
f(y) = -------
c y - a
and we wish to find the "fixed point" of f, i.e. solve the equation
y = f(y) ,
2
c y - 2 a y - b = 0 .
17
f(y) = ----
10 y
namely
0 17
10 0 .
Since the output must equal the input, the next term of y must always
be the integer part of the fixed point of the (homographic) function
defined by the matrix. To find this we can run a miniature
successive approximation for each term. For example, the arbitrary
guess that y = 3 gives f(y) = 17/30 , whose integer part is 0. The
average of y and f(y), whose integer part is 1, will be much closer,
being equivalent to a step of Newton's method. Now f(1) = 17/10, and
since the actual value always lies between y and f(y), 1 must be the
integer part of sqrt(17/10) and hence the first input and output
term. Outputing and inputing a 1:
0 17
10 10 0 1
7 -10 17
10 y + 10
y = --------- .
7 y - 10
3 1
0 17
10 10 0 1
11 7 -10 17 3
7 -11 40
3 3 1
0 17
10 10 0 1
11 7 -10 17 3
10 7 -11 40 3
10 -10 40
Then [f(2)] = 2,
2 3 3 1
0 17
10 10 0 1
11 7 -10 17 3
10 7 -11 40 3
10 10 -10 40 2
7 -10 27
sqrt(17/10) = 1 (3 3 2) .
a y + b
y = -------
c y - a
2
D = - a - b c
and
a + sqrt(-D)
y = ------------
c
2 - q x - 2 r
p x + q x + r = 0 -> x = ----------- .
2 p x + q
using
2
(coth t) + 1
------------- = coth 2t
2 coth t
with t = 1/2.
x y - 1
y = -------
y - x
. . . 7 5 3 1
0 -1
-1 0
1 0
0 1
y = - y .
y = - 1/y ,
. . . 7 5 3 1
-1 0 -1
-1 -1 0
1 1 0
1 0 1
. . . 7 5 3 1
-16 -3 -1 0 -1
-21 -4 -1 -1 0
21 4 1 1 0
16 3 1 0 1
Finally, both of the last two matrices have fixed points solidly
between 2 and 3, so we output a 2 in the z direction and input a
2 in the y direction.
. . . 7 5 3 1
-16 -3 -1 0 -1
-21 -4 -1 -1 0
26 5
21 4 1 1 0 2
16 3 1 0 1
-11 -2
11 2
4 1
Now the lefthand matrix has fixed point between 6 and 7, while 6
plugged into the righthand one gives 15/4. More input.
. . . 7 5 3 1
-16 -3 -1 0 -1
-21 -4 -1 -1 0
26 5
21 4 1 1 0 2
115 16 3 1 0 1
-79 -11 -2
79 11 2
29 4 1
. . . 7 5 3 1
-16 -3 -1 0 -1
-21 -4 -1 -1 0
26 5
21 4 1 1 0 2
115 16 3 1 0 1
-79 -11 -2
589 82
79 11 2 6
29 4 1
-95 -13
95 13
19 4
Now the lefthand matrix says 10, but not the right. Inputing 9 from
x confirms the 10 for y.
. . . 9 7 5 3 1
-16 -3 -1 0 -1
-21 -4 -1 -1 0
26 5
21 4 1 1 0 2
115 16 3 1 0 1
-79 -11 -2
589 82
79 11 2 6
265 29 4 1
-868 -95 -13
8945 979
868 95 13 10
175 19 4
-882 -95
882 95
125 29
For a while, this computation will be typical in that the output rate
will about match the input rate, while the matrix integers slowly
grow, but in this peculiar case, the output terms outgrow the input
terms, so that input must occur somewhat oftener to make up the
information rate.
Come to think of it, the schoolboy algorithm for square root is also
digit-at-a-time, but requires two inputs for each output to avoid
souring future outputs.
Worked Example
2
x + y
x <- ------
2 x
2 2
ax y + bxy + cy + dx + ex + f
------------------------------
2 2
gx y + hxy + iy + jx + kx + l
Then we can use the mechanism for simple arithmetic, starting with
the matrix
0 6
1 0
1 0
0 1
x y + 6
i.e. ------- , which, when y = x, is Newton's method for
x + y
x = sqrt(6). In the denominator, the choice of x + y
instead of 2x + 0y, for instance, will provide convenient symmetry
which will be preserved by the fact that both inputs (and the output)
will always be equal.
The four ratios amount to two 0s and two oos, indicating that we will
have to warm up the process before it produces terms automatically.
To get a term, we must estimate the integer part of the answer, which
we do simply by successive substitution using integer arithmetic.
Starting with a guess of 3, for instance:
3*3 + 6 2*2 + 6
------- = 2+ , ------- = 2+
3 + 3 2 + 2
so 2 is the first term, which we output and input for both x and y:
. . . 2
0 6
2 1 0
2 -2 6
1 0 2
1 0 1
0 1 -2
.
4 1 .
2 0 .
(We could have done this in any of the six possible orders.) Again
the ratios disagree, so we must take another guess and resubstitute it
until it stabilizes. Among the four ratios, 0/1 is the limit when
both inputs approach 0 (unrealisitic, they are greater than 1), the
two 1/0s are the limits when one input approaches 0 while the other
approaches oo (absurd, they are equal), and the 4/2 is the limit when
they both approach oo. Since the curve is asymptotically flat, this
last, lower left ratio, when finite, is the best first guess:
2
4*2 + (1+1)*2 + 0
------------------ = 2+
2
2*2 + (0+0)*2 + 1
. . . 2 2
0 6
2 1 0
2 -2 6
1 0 2
1 0 1
1 0 1 -2
0 1 -2
4 1 2
4 2 0
1 0 1
9 4
2 1
. . . 4 2 2
0 6
2 1 0
2 -2 6
1 0 2
1 0 1
1 0 1 -2
0 1 -2
4 1 2
4 2 0
4 1 0 1
2 0 2
9 4 4
9 2 1
4 1 0
40 9
18 4
Now we are cooking, since the three ratios, 40/18, 9/4, and 2/1, all
say that the next term is 2, and since everything is positive, the
true value must be between the greatest and least ratio. Pumping
through this 2, 2 4 2 2
0 6
2 1 0
2 -2 6
1 0 2
1 0 1
1 0 1 -2
0 1 -2
4 1 2
4 2 0
4 1 0 1
2 0 2
9 4 4
9 2 1
9 4 1 0
2 1 0
40 9 2
40 18 4
9 4 1
.
89 40 .
20 9 .
40 18
9 4
4 2
1 0
89 40
20 9
9 4
2 1
4 2
1 0
9 4
2 1
before, right after processing the second term. Since the matrix
alone determines its future inputs, a repetition immediately
implies a loop, proving
sqrt(6) = 2 2 (4 2 4 2) = 2 (2 4) .
4 1
-- = 1 + ---------
pi 4
3 + ---------
9
5 + ---------
16
7 + ---------
.
.
.
. . . 100 81 64 49 36 25 16 9 4 1 (1)
21 19 17 15 13 11 9 7 5 3 1
12 4 0 4
24 4 1 1 0 3
4 0 1
-------
555 51 6 1
16416 1044 79 7 1 0 7
1008 72 2 2
----------
98692840 3891940 169621 8261 456 29
308476 12166 15
307502 12107 1
974 59
The differences between this algorithm and the one for regular
continued fractions (all 1s in the numerators):
355
3 7 15 1 = 3 7 16 = --- = 3.1415929...
113
8261 456
518 28
tells us that the next term is between 15.9 and 16.3 so we could
(incorrectly) guess that the next term was 16:
. . . 100 81 64 49 36 25 16 9 4 1 (1)
21 19 17 15 13 11 9 7 5 3 1
12 4 0 4
24 4 1 1 0 3
4 0 1
-------
555 51 6 1
16416 1044 79 7 1 0 7
1008 72 2 2
----------
8261 456 29
-974 -59
Although this gets us an earlier output, the next three input terms
still fail to get us that big term near 300--not until the ingestion
of the pair
196
29
do we get our desired -294. After that, five more
inputs yield the three small outputs 3, -4, 5. (Small terms contain
less information and therefore come out quicker.) This data is based
on the assumption of an output whenever the nearest integers to both
the upper and lower limits are equal.
. . . 5 -4 3 -294 16 7 3
22 3 1 0
113 7 1 0 1 3
-14093 -4703 16 1 0 7
-881 -294 1 0 15
-878 -293 1
-3 -1 292
33 7 -2 -1 1
19 4 -1 0 1
14 3 1
5 1 2
4 1 1
1 0
7 -2 7 y - 2
= -------
4 -1 4 y - 1
which seems to say "between 7/4 and 2", thus foreordaining an output
of 1. Beware denominator elements of opposite sign! y between 0
and oo actually says "OUTSIDE 7/4 and 2", with a singularity at
y = 1/4. y must be outside 0 and 1/3 to assure an output of 1
(as was the case).
. . . a+b . . . . . . b 0 a . . .
==
(a+b)p+q p q (a+b)p+q p ap+q p q
i.e. the last two adjacent elements are in the same state either way.
0 0 x . . . = 0+x . . . = x . . .
e = 2 (1 2k+2 1) = 1 0 1 (1 2k+2 1) = (1 2k 1)
4
- = 1 2 8 3 (1 1 1 k+1 7 1 k+1 2)
e
= 1 2 1 0 7 1 0 2 (1 1 1 k+1 7 1 k+1 2)
= 1 2 (1 k 7 1 k 2 1 1) .
17
sqrt(--) = 1 (3 3 2) = (1 3 3 1 0)
10
. . . 1 2 3 4 5 0 -5 -4 -3 -2 -1 . . . = . . . 0 . . .
. . . -1 1 -1 1 -1 1 . . .
But these can be detected when they begin, whereupon you can shut
of output until they stop. You can also simply discard three pairs
at a time, since the only effect is to negate the whole state matrix:
. . . 1 -1 1 -1 1 -1 . . .
-p -q q-p -p q q-p p q .
R notation
Conversion to Decimal
. . . 1 15 7 3
22 3 1 0
7 1 0 1 3
******
150 10 0
106 7 1 1
*******
440 30
106 7 4
*******
180 160 20
113 106 7 1
********
670 540
113 106
That is, just follow the recipe for the homographic function of one
argument, except on output, you multiply by 10 after the subtraction,
instead of reciprocating. On paper, this necessitates recopying the
denominators, which resembles the outputing of 0. Thus, a decimal
fraction can be thought of as a continued fraction with two terms for
every digit. The denominators are the decimal digits alternated with
0s, while the numerators are alternately 1 and 10. On output, the gcd
of the matrix can be multiplied by a divisor of 10. This can be
detected simply by maintaining modulo 10 versions of the two
denominator coefficients. In the special case that the input
continued fraction is regular (as above), only a finite number of such
reductions can occur, corresponding to the total number of factors of
2 and 5 that the initial denominator coefficients shared in common.
Thus, although there is little reduction to be gained in the regular
case, there is also little effort-- you need only count the 2s and 5s
in the gcd of the initial denominator terms, and cancel out at most
one of each with each output multiplier of 10.
. . .
Conflicting Notations
oo
matrices
left to right.
Approximations
The comparison rule can also follow from the rule for subtracting two
continued fractions with zero or more initial terms in common. If
w = a b c ... p x
and
y = a b c ... p z ,
1 0
0 1 .
Then
u (x - z)
w - y = -----------------
(Dx + d) (Dz + d)
.685 = 0 1 2 3+
and
.695 = 0 1 2 5+
through the first term where they differ. By 3+ we mean some number
greater than 3 but less than 4, and similarly for 5+. From this
comparison rule, all of the numbers in the interval have continued
fractions = 0 1 2 x, where x is in the interval (3+, 5+], what ever
those two numbers happen to be. The simplest number in this interval
is 4. The simplest rational in [.685, .695) is therefore the number
whose continued fraction is
0 1 2 4 = 9/13 .(692307)
so as few as 13 people may have been polled, provided that they all
responded. This rationale is amplified on the page after next.
Of course, since we really only wanted the denominator 13, we could have
skipped the computation of the numerator 9:
4 2 1 0
13 3 1 1 0 1 ,
.695 -.010
1.000 .000 0
.695 -.010 1
.305 +.010 2
.085 -.030 3
.050 +.100
stopping when the magnitude of the interval width exceeds the last remainder.
At this stage, the last term would have been different, had we used the
other endpoint. But we can easily switch over to computing the other
endpoint by adding in the interval widths on whichever two consecutive
lines we wish:
.695 -.010
1.000 .000 0
.695 -.010 1
.305 +.010 2 .315
.085 -.030 3 .055 5
.050 +.100 .040
as required.
But C + 1/A and C + 1/B are what you get by prefixing the term C to
the continued fractions for A and B. This means that in determining
which of two (terminating) continued fractions represents the simpler
rational, we can ignore any initial sequence of terms that they have
in common. The continued fraction of the simplest rational included
in an interval consists of this common initial term sequence, to which
is appended the continued fraction of the simplest rational in the
interval determined by erasing the common initial sequence from the
original endpoints.
The reason Recipe #2 sounds easier is that it avoids the question of how
to do the arithmetic. When it comes down to doing the work, Recipe #1
will save you plenty.
.3120 +.0005
1.000 .0000 0
.3120 +.0005 3
.0640 -.0015 4
.0560 +.0065 1 .0625
.0080 -.0080 7 .0000 oo
.0000 +.0625
.312 = 0 3 4 1 7
and
.3125 = 0 3 4 1 oo = 0 3 5 .
0 3 4 1 8 = 44/141 = .312051...
Rounding rule: When you discard the tail of a continued fraction, you
effectively subtract from the last term retained the reciprocal of the
quantity represented by the tail. This reciprocal is greater than 1/2
iff the first term of the tail is 1, indicating that the last retained
term should be incremeted just when the first discarded term is 1.
Another way to look at it is that a final 1 can always be combined
into the preceding term, so why truncate just before a 1 when
truncating just after it will give a more accurate estimate with the
same number of terms?
pi = 3 7 15 1 292 1 1 1 2 1 . . .
and
355/113 = 3 7 15 1
Then, delete the first term and fold the left-hand portion over the 0:
146 1 15 7
146 1 1 1 2 1 . . . .
Because the upper continued fraction numerically exceeds the lower one
(by the comparison rule), 52163/16604 = 3 7 15 1 146 is the next better
approximation to pi after 355/113 (!) The improvement, however, is
microscopic: less than 2 parts in a billion.
z = a a ... a a a ...
0 1 i i+1 i+2
by discarding the terms beginning with term i+2. How far can we reduce term
i+1 before it would be better to simply discard it too? Define
N
- = a a ... a x = a a ...
D 0 1 i i+1 i+2
n
- = a a ... a
d 0 1 i-1
Nd - Dn = u (u = +or- 1)
Then
Nx + n
z = ------
Dx + d
N n a 1 a 1 a 1
( ) = ( 0 ) ( 1 ) ... ( i )
D d 1 0 1 0 1 0
N D a 1 a 1 a 1
( ) = ( i ) ... ( 1 ) ( 0 )
n d 1 0 1 0 1 0
implying
D
- = a ... a a
d i 2 1
Nx + n Np + n u (x - p)
------ - ------ = ----------------
Dx + d Dp + d (Dx + d)(Dp + d)
The crossover point is when these two errors are equal, i.e., when
d
p + - = x - p
D
or
Continued Logarithms
11111 100/2.54 -> 50/2.54 -> 25/2.54 -> 25/5.08 -> 25/10.16 -> 25/20.32
0 25/20.32 - 1 = 4.68/20.32
0 5.08/4.68 - 1 = .40/4.68
0 1.17/.80 - 1 = .37/.80
0 .40/.37 - 1 = .03/.37
0 .37/.24 - 1 = .13/.24
0 .24/.13 - 1 = .11/.13
0 .13/.11 - 1 = .02/.11
0 .11/.08 - 1 = .03/.08
0 .04/.03 - 1 = .01/.03
0 .03/.02 - 1 = .01/.02
0 .01/.01 - 1 = 0
oo
5
100 5 2
---- = 2 + ---------
2.54 2
2 2
2 + ---------
3
3 2
2 + ---------
.
.
.
1
1 2
2 + ---------
1
2 .
But until that deviation occurs (maybe never), the first term
of the continued fraction is in doubt. A partial solution to
this problem is to forcibly egest a 2 when it becomes clear
that this module is unduly stuck. If we are wrong, the gigantic
term will simply come out negative. What we would like to
say now is "regard my next term as infinite until further
notice", hoping that we are indeed through. But this is not
enough for the functions which depend on our output, to
which they will eventually become extremely sensitive. They
will need to know "just how close to infinity are you?", but
we, faced with an ever growing number with oscillating sign,
have no way to answer. We do not even know when we should
input more 2s (at least we hope they are 2s). The information-
drivenness has broken down.
a + b a
y between ----- and - ,
c + d c
provided c+d and d have the same sign. If both of these quantities
are >= 2, y can emit a 1 and halve itself by doubling c and d, or if
possible, halving a and b. If the two ratios lie between 1 and 2, y
decrements itself by subtracting c from a and d form b, then
reciprocates itself by interchanging a with c and b with d and finally
announces all this by emitting a 0.
6
y = - .
y
0 6
1 0
0 3
2 0 1
2 2 0 10
1 -2 3
Here the assumption y >= 2 fulfills itself, while 1 <= y < 2 will
drive
2 y + 2
-------
y - 2
0 3
2 1 0 10
2 -2 3 1
0 3
4 1 0 10
8 -4 3 11
0 3
4 1 0 10
4 8 -4 3 110
1 -4 5
Here y must be > 2 (in fact > 4) to avoid chasing the negative root,
and this time we can process the 1 by halving a and b, then halving
b and d.
0 3
4 1 0 10
2 2 -4 3 110
1 -2 5 1
But the matrix was in this state right after emitting the first 0,
so
sqrt(6) = 10(1101) .
In computational practice, we will need two other words besides 1 and
0 in the language. For negative numbers we could have either "-" for
"I negated myself" or "+" for "I incremented myself". For fractions
initially < 1, "*" would mean "I doubled myself", the opposite of "1".
Also, for finite inputs, we formally require an end-of-string mark,
which is logically oo, since the empty continued logarithm, like the
empty continued fraction, represents +or- oo. Continued logs can also
represent oo as an endless burst of 1s, if it results from
nonterminating inputs.
Architecture
After our octet has received about 2n inputs and produced about n
outputs, each of our registers will contain about n/4 significant
bits. Carry times will grow as log n, so our quotient or product or
whatever will have taken time proportional to n log n, like the FFT
algorithms, but with a far smaller constant of proportionality.
The four main advantages of this scheme over the FFT schemes are:
1) Simplicity--it is hardly more than a "smart memory", with a minimal
form of processor distributed throughout.
The key idea is that with every bit of storage there be the associated
logic to operate on that bit. The continued logarithm formulation
restricts the number of data paths to a conceivably practical level.