A Spigot Algorithm for the Digits of π Author(s)

A Spigot Algorithm for the Digits of π
Author(s): Stanley Rabinowitz and Stan Wagon
Source: The American Mathematical Monthly, Vol. 102, No. 3 (Mar., 1995), pp. 195-203
Published by: Mathematical Association of America
Stable URL: http://www.jstor.org/stable/2975006
Accessed: 09-03-2015 18:17 UTC
Your use of the JSTOR archive indicates your acceptance of the Terms & Conditions of Use, available at http://www.jstor.org/page/
info/about/policies/terms.jsp
JSTOR is a not-for-profit service that helps scholars, researchers, and students discover, use, and build upon a wide range of content
in a trusted digital archive. We use information technology and tools to increase productivity and facilitate new forms of scholarship.
For more information about JSTOR, please contact [email protected].
Mathematical Association of America is collaborating with JSTOR to digitize, preserve and extend access to The American
Mathematical Monthly.
http://www.jstor.org
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
Remainders
o*30>3
X -,.2 liS\>
4:
I
<t igi loiX8Ni ..MQ>.\\2RN42QN/320.
} S 1
-
I
Vll:
_
iS
|
A SpigotAlgorithmfor the Digits of
s
Stanley Rabinowitz and Stan Wagon
I DW
Dl
-
-
1|
.11
1
_
. 111
|
.. 1.
.
I
1
1
..
11111111
__
It is remarkablethat the algorithmillustratedin Table 1, which uses no floatingpoint arithmetic,produces the digits of 77.The algorithmstarts with some 2sSin
columns headed by the fractions shown. Each entry is multiplied by 10. Then,
startingfrom the rightSthe entries are reducedmodulo den,where the head of the
column is num/den7producinga quotient q and remainderr. The remainderis
left in place and q X num is carried one column left. This reduce-and-carryis
continuedall the way leX. The tens digit of the lehmost result is the next digit of
7r. The process continues with the multiplicationof the remaindersby 10 the
reductionsmodulo the denominators,and the augmentedcarrying.
TABLE
1. The workings of an algorithm that produces digits of Gr The dashed line indicates the key
stept starting from the rlght, elltries are reduced modulo the denominator of the column head
(25, 23, 2l, . . . 1resp.), with the quotients, aRer multiplication by the numerator (12, 11>10 . . . carried
left. For example, the 20 in the l99'scolumn yields a remainder of 1 and a left carry of 1 9 = 9. After
the leftmost carriesSthe tens digits are 3, 1, 4 1. To get more digits of v one must start with a longer
string of 2s.
)7
2
3
1
s
7
3
2
2
2
2
20 20 20 20
3 F;s < s *So i;2- s t-
Digits
of X
hitiMim
x 10
Carry
0 2Q 20
1x+12+20+33
40 53
)13
3
1 3
Remainders
30 10 30
x 10
4Fk +11 +24 +30
Carry
34 60
)41
81
1 0
ltemainders
10 10 0
x 10
+2 +9
1S+4
Carry
14 12 9
x 10
Cawy
8
9 10 11 12
5
6
7
4
9 11 13 15 17 19 21 23 2s
2
2
2
2
2
2
2
2
2
20 t20 20 20 20 N 20 20 20
=
H -s¢* *. +9-X83s .*i s e >*
*s+,at*'s .+>
40 30 100 10 130 120 10 200 200 2
;72+150+132.
+40+65+48 +98+
80 9S 148 108 218 192 160 332 296626.*
8
5
8 17 20 > (yt
5
4
3
5
30 S0 S0 40 80 S0 80 170 200 0
+88
+40 +40 +4X +43 + 64 +0+12Q
258 200 0
70 90 92 103 144 140 2
4 10 6 16 0
4 12 9
0
0
0
0 40 120 90 40 100 60 160 0
_
+24 +E +B4 +63 +48 +72 +60 +66 S
24 SS 124 183 138 112 160 126 16Q o
This algorithmis a 4spigot algorithm:it pumps out digits one at a time and
does not use the digitsafter they are computed.Moreover the digits are generated
without any use of high-precision(or low-precision)operations on floating-point
real numbers; the entire algorithm uses only ordinary integer arithmetic on
lg95]
A SPIGOT ALGORITHM FOR THE DIGITS OF
v
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
195
where
(2 lt the
* * *az)b(the
thls s) iSare
jUStnonnegat1ve
a restatement
intege>.
of the
If inftnite
0 < < i for
series
i > E18lt the
as
digitsof v requires
relativelysmall integers.For example,to obtain the first
only arithmetic operations on integers less than 600v000v000.Although highprecision fleating-pointroutines are built up from integer operationsXthe algorithmsin this paper are quite simple and do not simulatefloating-pointcomputa57000
.
tlons.
In order to motivatethe r-alWrithm7we fint discussthe much simplercase vf
eaa which a spigot algorithmwas discoveredby Sale [Salel. His alwrithm is the
basis of the discussionin §1.
1. A NUMBERSYSTEMIN WEIICHes DIATS ARE PECODIC. A real number?sdecimalrepresentationmaybe interpretedas an infinitelynested expression;
s
. ?
gXvewV1
WAA111H
vos
t + Ll (4 +
4-1b41421356w*w
3
1 (t +
t
(4 +
t
)))))
(2 + 1 (t +
Some interestingand useful representationsmay be obtained if we ange the
usingthe
. . . ). For exampleX
1ln 110
base-sequence,whichin the case aboveis ( 3ln 1lO,
represenbase b = (2 3, 1, 5 X. *) yields the followingform, called a m-radw
tation (see [Knu §4.1]).
ao
+
2tal
+
3(aX
+
4(a3
+
(a
+
X-(as
+
)))))
+*
representationis called rlar. Msed-radw representationswill be denoted by
(0;a1Xa2,a3a4X...)b. For base b, evew positive real number has a regular
representationand representationsare unique providedwe exclude representations that terminate with maximal digits (otherwise bt exampley =
(Q;1 0 Q . *+) = (0 QS2 3 5 6 * )b)Sim now on and for all basesSwe exclude
such repwsentations The proof of the fbllowingIwmmais in Appendz 1.
4?
SttcubrS
Lemma t(a)- It l > 1 (0 0 0 0 . 0 0 s a+l } * * )b <
1)bt <
a4
*
a3
(0; 41
usingthe mixed-rad base b are unique.
(b). Representations
(c)* The tntegerpart of (a 1 a2 a3 47 * **)h is aO and the ctional part S
Ili?
14
a2?
(°talta2a4-3a44*w-)b
In this number system some irrationalsbecome periodic. For
twamplet
e=
1 + 1 (1 + 2(1 + 3 (1 + 4(1 + 5(1 + * * * )))))* Rationalnumbersin this systemcorrespondto digit-sequencesthat terminate(Appendis 1 Lemma2).
The decimaldigits of a real numberX in [0X10) can be obtainedby takingthe
integerpart of x multlplyingits Eadiona1partby 10 takingthe integerpart of the
result multiplythe resuItingfractionalpart by 10 and so on. In some med-radz
bases? this is especially simple. If x-(a0; a 2 . . 2 n)b then lOx (1Q0; 10a1 10a lOa3 . . ., lOa,)b. The latter may not be a regular ewression
some digitsmaybe too big. But we can decreasedigits by reducingthem modulo z
where i is the denominatorof the correspondingelement of b. Startingthese
reductions at the right end we carry the quotients left, eventuallygetting the
reWlarrepresentationof 10X.Thus multiplpngby 10 is igorithmlcallyst:raightforward Taking the integer and ffactlonal pa"s for b-representationsis also easyS
thanksto LemmalEc).
A SPIT
ALOORIT
FOR THE DIGITS OF
X
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
[March
0.281826
0.81826
2.81826
0.18281826
0.8281826
2.718281826
7.18281826
8.281826
1.8281826
.........
........
......
.............
........
......0 10100 110
01010
20 11
10
0 302030
11231010
1403040
3310
1 50
420
1 14 10
160 5
40
2120
6 10
1 30
41
2 3030 10
01
3 02020
20
2 1210
2 2 001
90
00 10
99 100
1100
10
101010
111
We can now give the algorithmto get the first n base-10 digits of e. A proof of
correctness-the error analysisshowingthat n + 2 mixed-radixdigits suffice1 to
get n base-10 digits-is given as Lemma3 in Appendix1.
Algorithme-spigot
1. Initialize:
Let the first digit be 2 and initializean arrayA of length n + 1 to
(1,1,1,...,1).
2. Repeat n - 1 times:
Multiply
by 10: Multiplyeach entryof A by 10.
Takethefractionalpart:Startingfrom the right, reduce the ith entry of A
modulo i + 1, carryingthe quotientone place left.
Outputthenextdigit:The final quotient is the next digit of e.
The first few steps of this algorithm,starting with an array of 10 ls (this
correspondsto 11 mixed-radixdigits, good for 9 digits of e; only 5 are shown),are
displayedin Table 2.
TABLE2. The workingsof a spigot algorithmfor the digits of e (in bold). The reductionsin the
columnheaded 1 are performedmodulo i. The leftmostbase-10real numbersare the valuesof the
rowsviewedas mixed-radixrepresentations.Since only 11 mixed-radixdigitsstartthe algorithm,the
firstbase-10numberis only an approximation
to e.
Base 10
carries
carries
7
1
1/
1/
1/4
1/5
l/6
1/7
1/8
1/9
+3
+3
+2
+1
+1
+1
+1
+1
+0
--
14
13
12
11
11
11
11
11
10
10
+3
+0
+3
+9
+6
+4
+2
+0
+9
19
56
44
32
20
+3
Sd9
id0
29
90
i9
3
czaa
cames
8
2
10
3
86
s!9
+
<
+2
+40
16
19
38
43
22
20
+7
+8
+9
+4
+2
+0
27
38
49
64
32
20
+5
+6
5
16
8
3
l/lo
9
/11
_
100
-10
-9
100
2. A SPIGOTFOR DIGITSOF . The ideas of §1 lead to a spigot algorithmfor
7r,but there are additionalcomplexitiesand additionalinterestingquestions that
distinguish7r from e. Our startingpoint is the followingmoderatelywell-known
1Any digit-producing algorithm for a presumed-normal number x suffers from a drawback that,
although unlikely, can impinge on the result. If x is between 1 and 10 and the algorithm says that the
first 100 digits of x are, say, 4, 6, 5, 0, 7, . . ., 3, 9, 9, 9, 9, 9 then one cannot be sure that the last 6 digits
are correct. They will be the digits of a certain approximation to x that is within 5 10-1°° of the true
value. One cannot simply go farther until a non-9 is reached, because memory allocations must be made
in advance. The user must realize that a terminating string of 9s is a red flag concerning those digits
and even with no 9s, the last digit might be incorrect. In practice, one might ask for, say, 6 extra digits,
reducing the odds of this problem to one in a million.
1995]
A SPIGOT ALGORITHM FOR THE DIGITS OF 7T
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
197
.
serles:
7
(it) 2
.E0(2i + 1)t
This series can be derivedfrom the Wallis productfor 1r;another approachuses
an accelerationtechniquecalled Euler's transformapplied to the series 7r= 4 These proofs, together with three others and references to
3- + 54 - 4 +....
earliersources,may be found in [Li].We let kft denote the product1 *3 *5 * k
for odd integers k; then the series is equivalentto
1 *2 * 3
1 *2
1
i!
X
7r
3 5 7
3 5
3
2 iE (2i+1)!!
which expandsto become
11
ar
2 =1+ 3t1+
4
3
2
5 1+ 7 1+ 9(1+---)
.
This last expressionleads to the mixed-radixbase c = (-, 5-,37, 49, . . . ), with respect
to which sr is simply (2;2, 2, 2, 2, 2, . . . )c For a regularrepresentationin base c,
the digit in the ith place must lie in the interval[0,2i]. Unfortunately,base c is less
accommodatingthan b.
Lemma 4 (Proof in Appendix 1). The base-c numberwith maximaldigits,
of the form
(0; 2, 4, 6, 8, . . . )7 represents2; hence regularrepresentations
(0; a7b, c, . . . )c lie betweenOand 2.
Lemma 4 implies that c-representations are not unique. For example,
= 2 _ 23= 43, whence (0;0,2,3,4,...)c = 32= (0;2,0c0,0c...)C.
(0,0,4,6,8,...)
More relevantalgorithmically,integerand fractionalpartsusing c are not straightforward,as they are for b. The integer part of (aO,al, a27. +. )c is either aO or
aO+ 1 accordingas (0; a1, a2, . . . ) is in [0, 1) or [1, 2). This problemis surmounted
by leaving the units digit of aOin place duringthe next iteration and calling the
tens digit of aO a predigat.The predigits must be temporarilyheld because
occasionally(once every 20 iterations,roughly)the next predigitis a 10; this will
greater than 10 and,
happen when the carrySwhich is between 0 and
simultaneously,the leftover units digit of aO is 9 which becomes 9Q in the
multiply-by-10step. This event requiresthat the held numberbe increasedby 1
before being released. Specific details of the algorithmfollow;the presentationat
the beginning of this paper sidestepped the problem of the occasional 10. The
proof that [lOn/3J mixed-radixdigits suffice for n digits of v is in Appendix 1
(Lemma5). Appendix2 containsa Pascal implementationof this algorithm.
197
iS
AlgorithmTr-spigot
Let A = (2, 2, 2, 2, . . ., 2) be an arrayof length LlOn/3j.
1. InitiaZize:
2. Repeat n times:
by10: Multiplyeach entry of A by 10.
Multiply
form:Startingfrom the right,reduce the ith element of A
PutA intoregular
(correspondingto c-entry (i- 1)/(2i- 1)) modulo 2i- 1, to get a
quotient q and a remainderr. Leave r in place and carryq(i - 1) one
place left. The last integer carried(from the position where i - 1 = 2)
maybe as large as 19.
198
A SPIGOT ALGORITHM FOR THE DIGITS OF
v
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
[March
Get the next predigit: Reduce the leftmost entry of A (which is at most
109[- 9 * 10 + 19]) modulo 10. The quotient qXis the new predigitof
r the remainderstayingin place.
Adjustthepredigits:If q is neither 9 nor 10 release the held predigitsas true
digitsof sr and hold q. If q is 9, add q to the queue of held predigits.If
q is 10 then:
. set the currentpredigitto Oand hold it;
. increase all other held predigitsby 1 (9 becomes 0);
* release as true digits vf v all but the currentheld predigit
This algorithmuses only integer arithmeticand is easy to program.The table at
the beginningof the paper shows it in action startingwith 13 mised-radixdigitsof
note that the
1r(good for 4 base-10digits).To clarib the workingof the algorithmX
(Enite) first row of Table 1 is a mrsed-radwrepresentationof 3.1414796..*
the second row represents31.414796..., the fifth row represents1.414796..., the
sixth row is 14.14796. . . * the ninth row is 4.14796. . . * and so on. Table 3 shows
the result of a computationusing a largerinitialarray the holdingaspect does not
become relevantuntil the 32nd digit.
TABLE3. The actual digits of T ottom) comparedto the sequence of leftmostbase-c digits for 35
iterationswith a startingarrayof 116 2s (good br 35 digits).At the 32nd iterationa 102 shows up
yieldinga predigitof 10.
30 13 41 15 58 92 26 64 53 35 58 89 97 78 92 32 23 38 84 4S 62 26 63 42 33 38 82 32 27 78 94 49 102 28 87
3 1 4 1 S 9 2 6 S 3 S 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 S 0 2 8
To get 5+000digitsof
We repeat that the algorithmuses only integeroperationsX
The algo600,00Q000.
less
than
v requires only integer arithmeticon numbers
rithm leads naturallyto the question of improvingit to one that is essentiallyas
simple as e-spigot.
Question.Is there a base d of rationalssuch that xr has a d-representationthat is
periodic,or an arithmeticprogression,and such that aOis alwaysthe integerpart
of (an; al, a2, *. *)d?
32] has discovereda series for
Gosper
close to spigot-perfection:
p-
[S7
T
3
1
608
1 2*3
60 7 * 8 - 3 3
r
that brin us tantalizingly
3*5
1 2*3
60 7 8 3 lQ 11 3
4*7
3 5
1 2-3
23 + *-60 7 *8 3 10 * 11 3 13 14 *3
He obtainedthis series by using a refinementof the Euler transformon 4 - 3- +
Gosper's series leads to the base d = (6l0 1G8, 3130 52867++,) with
45 - 47 + ....
respect to which sr is (3 8, 13 18, . . * ). A computation shows that
. . ., a substantialimprovementover the 2 that
. . . )d-1.092
(0;59x 16713297545x
arose for c. Under the usual randomnessassumptionfor rT'Sdigits the odds of a
19951
A SPIGQT ALGORITHM FOR E
DIGITS OF
v
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
lg9
bad predigitin base c are 1 in 20, while in base d they decrease to less than 1 in
110;this is because a d-predigitof 10 occursonly when the remainderis a 9 (which
becomes 90) and the carryis a 10. The formerhappens lONo of the time, while the
latter happensno more than once in 11 iterationsbecause the carryis the integer
part of a real between 0 and 10.93. So base d is within 1% of spigot-perfection.
Because Gosper'sseries convergesmore quicklythan the one we used, it has less
memoryrequirements:n digits of rr requirean initial arrayof length n; however,
the arithmeticon the arraywill involveintegerslargerthan those in an arrayof the
same slze uslng rase c.
One way to improvethe Gosper-seriesapproachis to reduce the fractionsin d
to lowest terms. Then the regular numberwith maximaldigits is (0; 59, 27, 21,
38, ... )d which equals 1.0000476468.... It is not hard to see that the regular
representationof qr is unchangedin this new base. However,the work expended
in reducingto lowest terms outweighsthe gain made in reducingthe numberof
times a 10 appearsas a predigit.Thus it is likely that an affirmativeanswerto the
question above is of more theoreticalthan practicalinterest.
The spigot algorithm for 7r is by no means competitive with the recently
discoveredfast algorithms(due to the Borweinbrothers,the Chudnovskybrothers,
and others) that have been used to compute hundredsof millions of digits of 7r
(see [BBB]). But the spigot algorithmdoes have the advantageof avoiding all
floating-pointcomputations;thus it is easily implementedon a home computer
where it can produce thousandsof digits in a few minutes. Moreover,it gives the
producethe result in binaryor
result directlyin base 10 (most other gr-algorithms
to obtain decimal digits).
made
must
be
pass
a
second
and
format
some internal
The algorithmgiven here can be made to run faster by outputting multiple
digits at a time. For example,to get five decimal digits at a time, simplycompute
the digits of 7r using base 100,000.This can be done by multiplyingby 100,000
instead of 10 in the main step. The integer part is then the next "digit"in base
.
*
100,000.
If one is workingin base 100,000and knowsin advancethat the portionof digits
to be computeddoes not contain the string00000, then one can omit the lengthy
part of the algorithmthat adjuststhe predigits.This can lead to an exceedingly
short computerprogram.For example,Rabinowitz[Rab]used this idea to exhibita
14-line Fortranprogramthat outputs 1,000 decimaldigits of 7r.
Finally, we mention that the algorithmcan be parallelized,in which case it
becomes blindinglyfast up to about 10,000digits.
For examplesof spigot algorithmsfor other functions,see [Abd].
APPENDIX1. FIVELEMMAS
Lemmal(a). If i2 l,(O;O,O,...,O,ai,ai+l,...)b<
a4
. . . )b <
l;inparticular,(O;al,a2,a3,
1
baseb areunique.
usingmaxed-radix
(b). Representations
part is
(c). Theintegerpart of (aO; al, a2, a3, a4, . . . )b is a,, and thefractionaZ
(°; al
a4
a2' a3
* * * )b-
Proof:(a). It suffices to prove that Ek=i+l(k - l)/k!= l/i!, which follows from
the fact thatthe series telescopes to:
( 1
200
1
)
(
1
1
)
(
1
1
A SPIGOT ALGORITHM FOR THE DIGITS OF 7T
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
)
[March
(b)* Suppose (aO; al, a2, a3, a41 * * * )b and (cO;C1, C2, C3, C4, . . . )b represent the
same real number.Then, for some i, O= Sk=idk/k!, where ldkl < k and di + °*
But then | di | /i! < Sk =i +1| dk| /k !, contradicting(a).
(c). This follows from (a).
Lemma2.
eventually
A
positive
number
is
rational
iff
its
digits
using
the
mixed-radix
b are
base
0.
The reverse direction is obvious. For the forward direction we use a
sublemma.
Proof:
Sublemma. For
such
that
any
n
*** +dt_3t(t
-
-
integers
d1t(t
-
t and
1)(t
1) + dt-2t
-
n,
with
<
O
2)
4
n
* 3
<
+
t!,
there
d2t(t
are
-
integers
1)(t
-
di
2)
in
5
[O,
* 4
i]
+
+ dt-lv
By inductionon t. If n < t! write n as qt + r with O < r < t and O < q <
By inductionthere is a sequence (d1, d2, . . . dt_3, dt_2) that is a solution
for q with respect to terms (t - 1)(t - 2)
4*
3,
and the like, whence
(d1,
d2,
. . . dt_3,
dt_2,
r) is a solutionfor n w.r.t.the terms t(t
1)(t
2)
* * * 4 * 3,
and the like.
Proof:
(t
-
1)!.
Returningto Lemma2's proof, supposea positiverationals/t is given.Use the
sublemma to express s(t - 1)! in the form d1t(t - 1)(t - 2)
4 * 3 + d2t
(t
1)(t
2)
***
5 * 4 +
**
+dt_3t(t
1) + dt_2t
+ dt_1.
Dividing by t! then
yields a representationof s/t as a sum of reciprocalsof factorialswith appropriately small coefficients,which is the same as a terminatingrepresentationin the
mixed-radixbase b.
Lemma
3.
The
algorithm
for
digits
of
e
is
correct.
It mustbe shownthat n + 2 mixed-radixdigitsof e suffice to get n base-10
digits of e. We first prove that if n 2 28 (= [lOel), then n mixed-radixdigits
suffice for n base-10 digits. Using n mixed-radixdigits means we are actually
gettingthe base-10 digitsof en = (2;1,1,1, . . . ,1) = LI.=ol/i!. Thus we must show
that
e en < S * 10-n (see footnote at beginningof paper).A geometricseries estimation of the tail of the series shows that e - en < 2/(n
+
1)!,
and then Stirling's
formulayields
Proof:
(n
+
1)!
n!
(n)
( 10)
If n < 28 then a direct computationof the digits shows that
digits suffice.
Lxmma
hence
4.
regular
The
base-c
number
representations
with
of
the
maximal
form
digits,
(O;
a,
b,
(0;
c,
. . . )c
2,
lie
4,
6,
8,
between
n
+
2
. . . ),
O
mixed-radix
represents
and
2;
2.
Insteadof givinga formalproof,we show how some Mathematica
computations led to the result (and a proof). In terms of series, the lemma states that
Proof:
,
(2i)i
!
_
2
=o(2i+,1)!!
1995]
A SPIGOT ALGORITHM FOR THE DIGITS OF XT
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
201
A roughcalculationshowedthat the sum is near 2. Then a rationalcomputationof
the remainders the differences between the partial sums and 2 yielded the
followingsequence.
_ 4
3' 5
1
16 16 32
32 512_
256
256
512
35 1 63 ' 231 1 429 ' 6435 1 12155 1 46189 ' 88179 *
The patternin these remainderswas found by dividingeach by the precedingone,
which yielded:
3 4 5 6
7
8
9 10 11
5' 7' 91 11' 13' 15' 17' 19' 21*
Inductionproves the pattern to be valid in general;it follows that the remainders
have the closed form 2n+l/(2n
+ 1) which convergesto 0, as claimed.
Ltmma 5. Thealgorithmfor digitsof
XT is
correct.
Proof: As for e, we look at XT - XTm
where 7rm= (2;2, 2, . . ., 2)c.This erroris the
tail of our main series for Tr:Li=m(i!)22i+1/(2i + 1)!. This tail is less than twice its
first term since each subsequentterm is less than half its predecessor,leadingus to
study m!22m+2/(2m + 1)!. Splittingthe denominatorinto evens and odds turns
this into: m! 22/(3 *5 ***(2m + 1)), which is less than 3-m!22/(2 *4 ***(2m)), or
1/(3 *2m-1). It is easy to see (using the fact that 130< loglo 2) that this last is less
than 5 * 10-n when m = 110n/3], as claimed.
APPENDIX2. PASCALCODE
The followingprogram,for which we are gratefulto Macalesterstudent Simeon
Simeonov,implementsthe algorithm7r-spigot.This code makesuse of the fact that
the queue of predigitsalwayshas a pile of 9s to the right of its leftmost member,
and so only this leftmostpredigitand the numberof 9s need be remembered.The
programcomputes1000 digitsof gTand requiresa versionof Pascalwith a longint
data type (32-bit integer).
Program Pi_Spigot;
const n
=1000;
len
=108n div 3;
var
i, j, k, q, x, nines,
predigit
: integer;
a : array[1..len]
of longint;
begin
for j := 1 to len do a[j]
:= 2;
{Start
with 2s}
nines
:= 0; predigit
:= 0
{First
predigit
is a 0}
for j := 1 to n do
begin q := 0;
for i := len downto 1 do
{Work backwards}
begin
x := 10*a[i]+q*i;
aCi] := x mod (2*i-1);
q := x div (2*i-1);
end;
a[1] := q mod 10; q := q div 10;
if q = 9 then nines
:= nines + 1
else
if q=10 then
202
A SPIGOT ALGORITHM FOR THE DIGITS OF XT
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
[March
Westford,
01886
St.
Paut
MN 55105
begin writeXpredigit+1);
for k := 1 to nines do write(O);
:= O
:= O; nines
predigit
end
else begin
predigit
*= q;
writeXpredigit),
if nines <> O then
begin
for k := 1 to nines do write(9);
nines
:= Q
end
end
{zeros}
end,
writetnSpredigit);
end.
(2.3) can sum many of the series that occur in
ADDEDIN PROOF The latest version of Maffiematica
this paper. It takes only a second or so to get 7r/2 as the sum of the crucial series at the beginning of
section 2, to get 1/il for the series in Lemma 1's proof, and to get 2 as the sum of the series in Lemma
4's proof.
REFERENCES
[Abd] S. Kamal Abdali, Algorithm 393 Special series summation with arbitrary precision, Comm.
ACM 13 (1970) 570.
[BBB] J. M. Borwein, P. B. BorweinS and D. H. Bailey Ramanujan, modular equations, and
approximations to pi, or HQWto compute one billion digits of pi, this Monthly96 (1989)
201-219
[Gos] R W. Gosper Acceleration of series, Meme no. 304, M.I.T Artificial Intelligence Laboratory
Cambridge, Mass., 1974.
[Knu3 D. E. Knuth, TheArt of ComputerProgrammingvolume 27 Reading, Mass. Addison-Wesley,
l981.
[Li]
J. C. R. Li, Problem E854, this Monthly56 (1949) 633-635.
[Rab] S. Rabinowitz, Abstract 863-11-482: A spigot algorithm for pi, AbstractsAmer Math.Societ 12
(1991) 30.
[Sale] A. H. J. SaleS The calculation of e to many significant digits, Comput.J. 11 {1968) 229-230.
MathPro
Press
P.O.Box7]3
ofMathematEcs
Department
College
Macalester
[email protected]
wagonUmacalstr.edu
lg9S]
FOR TFIE DIGITS OF 7T
A SPIGOT ALGORTTHhvI
This content downloaded from 158.135.191.86 on Mon, 09 Mar 2015 18:17:59 UTC
All use subject to JSTOR Terms and Conditions
203