Spigot algorithm explained

A spigot algorithm is an algorithm for computing the value of a transcendental number (such as or e) that generates the digits of the number sequentially from left to right providing increasing precision as the algorithm proceeds. Spigot algorithms also aim to minimize the amount of intermediate storage required. The name comes from the sense of the word "spigot" for a tap or valve controlling the flow of a liquid. Spigot algorithms can be contrasted with algorithms that store and process complete numbers to produce successively more accurate approximations to the desired transcendental.

Interest in spigot algorithms was spurred in the early days of computational mathematics by extreme constraints on memory, and such an algorithm for calculating the digits of e appeared in a paper by Sale in 1968.[1] In 1970, Abdali presented a more general algorithm to compute the sums of series in which the ratios of successive terms can be expressed as quotients of integer functions of term positions. This algorithm is applicable to many familiar series for trigonometric functions, logarithms, and transcendental numbers because these series satisfy the above condition.[2] The name "spigot algorithm" seems to have been coined by Stanley Rabinowitz and Stan Wagon, whose algorithm for calculating the digits of is sometimes referred to as "the spigot algorithm for ".[3]

The spigot algorithm of Rabinowitz and Wagon is bounded, in the sense that the number of terms of the infinite series that will be processed must be specified in advance. The term "streaming algorithm"[4] indicates an approach without this restriction. This allows the calculation to run indefinitely varying the amount of intermediate storage as the calculation progresses.

A variant of the spigot approach uses an algorithm which can be used to compute a single arbitrary digit of the transcendental without computing the preceding digits: an example is the Bailey–Borwein–Plouffe formula, a digit extraction algorithm for which produces base 16 digits. The inevitable truncation of the underlying infinite series of the algorithm means that the accuracy of the result may be limited by the number of terms calculated.

Example

This example illustrates the working of a spigot algorithm by calculating the binary digits of the natural logarithm of 2 using the identity

infty
ln(2)=\sum
k=1
1
k2k

.

To start calculating binary digits from, as an example, the 8th place we multiply this identity by 27 (since 7 = 8 − 1):

27ln(2)

infty
=2
k=1
1
k2k

.

We then divide the infinite sum into a "head", in which the exponents of 2 are greater than or equal to zero, and a "tail", in which the exponents of 2 are negative:

27ln(2)

7
=\sum
k=1
27-k
k
infty
+\sum
k=8
1
k2k-7

.

We are only interested in the fractional part of this value, so we can replace each of the summands in the "head" by

27-k
k

\bmod1=

27-k\bmodk
k

.

Calculating each of these terms and adding them to a running total where we again only keep the fractional part, we have:

kA = 27−kB = A mod kC = Sum of C mod 1
164000
232000
3161
4800
544
622
711

We add a few terms in the "tail", noting that the error introduced by truncating the sum is less than the final term:

kD = Sum of DMaximum error
8
9
10

Adding the "head" and the first few terms of the "tail" together we get:

27ln(2)\bmod1

64+
105
37
360

=0.10011100\ldots2+0.00011010\ldots2=0.1011\ldots2,

so the 8th to 11th binary digits in the binary expansion of ln(2) are 1, 0, 1, 1. Note that we have not calculated the values of the first seven binary digits – indeed, all information about them has been intentionally discarded by using modular arithmetic in the "head" sum.

The same approach can be used to calculate digits of the binary expansion of ln(2) starting from an arbitrary nth position. The number of terms in the "head" sum increases linearly with n, but the complexity of each term only increases with the logarithm of n if an efficient method of modular exponentiation is used. The precision of calculations and intermediate results and the number of terms taken from the "tail" sum are all independent of n, and only depend on the number of binary digits that are being calculated – single precision arithmetic can be used to calculate around 12 binary digits, regardless of the starting position.

Further reading

Notes and References

  1. Sale, AHJ . 1968 . The calculation of e to many significant digits . The Computer Journal. 11 . 2 . 229–230 . 10.1093/comjnl/11.2.229. free .
  2. Special Series Summation with Arbitrary Precision . Abdali, S Kamal . 1970 . Communications of the ACM . 13 . 9 . 570 . 10.1145/362736.362756.
  3. A Spigot Algorithm for the Digits of Pi. Rabinowitz. Stanley. Wagon. Stan. American Mathematical Monthly. 102. 1995. 195–203. 8 May 2013 . 10.2307/2975006 . 3. 2975006.
  4. Web site: Jeremy. Gibbons. 24 May 2004. Unbounded Spigot Algorithms for the Digits of Pi.