Spock: "Computer, compute to the last digit the value of pi."
- Wolf in the Fold (Star Trek)
This Java Script calculator implements a spigot algorithm that generates a stream of digits that represent π. Digits are generated in groups of four. Enter the number of digits desired and press the Compute button to write digits into a separate window. On invalid entries, a popup window will display an error message.
Return to Contents
The Java Script source code for this program can be viewed by using the View|Source command of your web browser.
You may use or modify this source code in any way you find useful, provided that you agree that the author has no warranty, obligations or liability. You must determine the suitability of this source code for your use.
Return to Contents
Archimedes' Constant, π
A procedure based on integer arithmetic for calculating the value of π to a large number digits, known as the spigot algorithm, was first published by Rabinowitz and Wagon [1]. A spigot algorithm produces its outputs incrementally, and does not reuse them after producing them. The algorithm uses only bounded integer arithmetic, is efficient, and allows concise implementations.
The execution time of the algorithm grows with order N2. The number of executions of its innermost loop is given by:
where N is the number of digits generated by the algorithm.7·(N/4)·[(N/4) + 1]
References
[1] A spigot algorithm for the Digits of π, Stanley Rabinowitz and Stan Wagon, Am. Math. Monthly, March 1995, 195-203
Return to Contents