The name comes from a "spigot", meaning a tap or valve controlling the flow of a liquid.
Interest in such algorithms was spurred in the early days of computational mathematics by extreme constraints on memory, and an algorithm for calculating the digits of e appears in a paper by Sale in 1968. The name "Spigot algorithm" appears 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 π".
The spigot algorithm of Rabinowitz and Wagon is bounded, in the sense that the number of required digits must be specified in advance. Jeremy Gibbons (2004) uses the term "streaming algorithm" to mean one which can be run indefinitely, without a prior bound. A further refinement is an algorithm which can compute a single arbitrary digit, without first computing the preceding digits: an example is the Bailey-Borwein-Plouffe formula, a digit extraction algorithm for π which produces hexadecimal digits.
To start calculating binary digits from, say, the 8th place we multiply this identity by 27(since 7 = 8 - 1):
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:
We are only interested in the fractional part of this value, so we can replace each of the summands in the "head" by
Calculating each of these terms and adding them to a running total where we again only keep the fractional part, we have:
k A = 27-k B = A mod k C = B / k Sum of C mod 1 1 64 0 0 0 2 32 0 0 0 3 16 1 1/3 1/3 4 8 0 0 1/3 5 4 4 4/5 2/15 6 2 2 1/3 7/15 7 1 1 1/7 64/105
We add a few terms in the "tail", noting that the error introduced by truncating the sum is less than the final term:
k D = 1/k2k-7 Sum of D Maximum error 8 1/16 1/16 1/16 9 1/36 13/144 1/36 10 1/80 37/360 1/80
Adding the "head" and the first few terms of the "tail" together we get:
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.
- Sale, AHJ (1968). "The calculation of e to many significant digits". The Computer Journal 11 (2): 229–230. doi:10.1093/comjnl/11.2.229. Retrieved 8 May 2013.
- Rabinowitz, Stanley; Wagon, Stan (1995). "A Spigot Algorithm for the Digits of Pi" (PDF). American Mathematical Monthly 102 (3): 195–203. doi:10.2307/2975006. Retrieved 8 May 2013.
- Gibbons, Jeremy (24 May 2004). "Unbounded Spigot Algorithms for the Digits of Pi" (PDF).