Generate arbitrary number of digits of Pi using a streaming algorithm.
PiDigits Implements the ‘Unbounded Spigot Algorithm for the Digits of Pi’ by Jeremy Gibbons. The paper describing this algorithm can be found at this URL.
>>> pip install --upgrade pidigits
To uninstall using pip.
>>> pip uninstall pidigits
PiDigits provides a generator function named piGenerator that yields the digits of Pi as needed. The streaming algorithm based on Lambert’s expression is used for the generator function.
>>> from pidigits import piGenerator >>> mypi = piGenerator() >>> first20pi = [next(mypi) for i in range(20)] >>> first20pi [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4]
Example: Verify the Feynman point.
>>> mypi = piGenerator() >>> first1001 = [next(mypi) for i in range(1001)] >>> feynman = first1001[762:768] >>> print feynman [9, 9, 9, 9, 9, 9]
Alternatively you can also use the getPi(n) function to get the first n digits of Pi.
>>> from pidigits import getPi >>> first20pi = getPi(20) >>> first20pi [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2, 3, 8, 4]
Alternate implementations of streaming algorithms based on Leibniz series and Gosper’s series are also available as generator functions piGenLeibniz and piGenGosper and corresponding getPiLeibniz and getPiGosper functions. These implementations are slower than the one based on Lambert’s series.
To run the tests, in the same directory as setup.py, first run:
>>> python setup.py develop
This will install the package in the ‘development’ mode. Then run the test cases:
>>> python setup.py test
Bug reports or suggestions are most welcome.
pidigits is licensed under Apache License 2.0.
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size pidigits-0.4.zip (15.1 kB)||File type Source||Python version None||Upload date||Hashes View|