Spigot algorithm

[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 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.

This example illustrates the working of a spigot algorithm by calculating the binary digits of the natural logarithm of 2 (sequence A068426 in the OEIS) using the identity To start calculating binary digits from, as an example, 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: We add a few terms in the "tail", noting that the error introduced by truncating the sum is less than the final term: 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.