Technical Correspondence
Letter: Fractions as Sequences of Integers
Vector 12:3, page 130
Transcribed from the printed issue; not yet reviewed. Read it in the PDF of the issue, page 130
The first problem considered here is that of converting decimal fractions to fractions F in base n. Here are two solutions; which to use depends on whether you are interested in the number of digits in the results, or in the closeness of the approximation. The inverse in each case is given by (÷n)⊥⌽ 0,F.
[0] z ← n fdiv f;t
[1] ⍝ z is fraction f expressed as n-ary fraction
[2] ⍝ n has form of left argument of encode
[3] z ← ⍳0 ⋄ →(0=⍴n)/0
[4] z ← (⌊t),(1↓n)f÷1|t ← f×↑n
tol ← 1E¯12
+F ← (5⍴5)fdiv 0.67296
3 1 4 0 2
(÷5)decode rotate 0,F
0.67264
+F ← (9 ⍴ 5)fdiv 0.67296
3 1 4 0 2 4 4 4 4
(÷5)⊥⌽0,F
0.672959488
[0] z ← n fdiv1 f;t;u
[1] ⍝ z is fraction f expressed as n-ary fraction
[2] ⍝ algorithm stops when tolerance has been achieved
[3] z ← ⌊ f ÷ u ← ÷ n
[4] L1:→(tol > t ← u|f)/0
[4] z ← ⌊ t ÷ u ← u ÷ n ⋄ f ← u|t ⋄ →L1
+F ← (5⍴5)fdiv1 0.67296
3 1 4 0 3
(÷5)⊥⌽0,F
0.67296
4 fdiv1 ÷ 3
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
A related problem is that of expressing a fraction as a sum of best-fitting unit fractions, the algorithm stopping when a given tolerance has been achieved. This is sometimes called Sylvester’s algorithm, and is coded:
[0] z ← frac f;t
[1] ⍝ z is fraction f expressed as sum of unit fractions
[2] z ← ⌈ ÷ f
[3] L1:→(tol > t ← f- ÷ ¯1↑z)/0
[4] z ← z,⌈÷f ← t ⋄ → L1
frac 0.67296
2 6 159 248438 1.234441339E11
Like the first algorithms above, this provides a way in which a fraction can thus be expressed as a sequence of integers. In particular the sequence corresponding to the fractional part of pi is