## Digits of PI (Part 2)

On the Python mailing list I got some great answers on how to generate the digits of PI. Here I sum them up.

**Solution 1**

Tichodroma forwarded me to http://mail.python.org/pipermail/edu-sig/2006-July/006810.html.

Quote:

Here's a generator I coded up based on a paper by Gibbons: It's simple to code, but I think you have to read the paper to figure out what it's doing. (I just translated some code, so I really can't tell you :-) In the paper, this was done in a lazy functional language. I was mostly interested to see how it would translate to a Python generator. # pi.py -- imlementation of Gibbons' spigot algorithm for pi # John Zelle 4-5-06 def pi_digits(): """generator for digits of pi""" q,r,t,k,n,l = 1,0,1,1,3,3 while True: if 4*q+r-t < n*t: yield n q,r,t,k,n,l = (10*q,10*(r-n*t),t,k,(10*(3*q+r))/t-10*n,l) else: q,r,t,k,n,l = (q*k,(2*q+r)*l,t*l,k+1,(q*(7*k+2)+r*l)/(t*l),l+2) Here it is in action: >>> import pi >>> digits = pi.pidigits() >>> for i in range(30): print digits.next(), ... 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 >>> Since this uses long ints, it slows down considerably after a few thousand digits. You might want to use psyco when generating really "deep" digits. --John

It generates the digits of PI one after the other. It works well bit if you want lots of digits, it gets really slow.

**Solution 2**

E. Woiski suggested using the library SymPy.

sudo apt-get install python-sympy

>>> from sympy.mpmath import mp >>> mp.dps = 1000 # number of digits >>> +mp.pi # str(mp.pi)

Very fast and simple. The only problem might be that you need to install sympy.

**Solution 3 (update, 20121128)**

One of my students called G. Szegedi came up with this solution:

from bigfloat import precision import bigfloat str_pi = str(bigfloat.atan2(+0.0,-0.0,precision(1000)))

With the bigfloat package you can do high precision floating-point arithmetic.

## Digits of PI (Part 1)

**Problem**

You want to work with the digits of PI. Why? For instance you want a new job (screenshot here if it got removed since then).

**Solution**

I like simple solutions. So instead of generating the digits, I simply fetched the data from the web. This is a fast, efficient, and painless approach of the problem :) Visit http://newton.ex.ac.uk/research/qsystems/collabs/pi/, where you can download several data files.

**For the lazy pigs**

I made a script that downloads the data, parses them, and returns the digits as a string. Here it is.

Usage (get the first 30 digits of PI after the dot):

#!/usr/bin/env python from jabbapylib.math import pi def main(): digits = pi.get_digits_of(pi.PI3) # get 10^3 = 1000 digits print digits[:30] if __name__ == "__main__": main()

Output:

141592653589793238462643383279

jabbapylib is here

## First 15 digits of PI

Look at this verse:

How I want a drink alcoholic of course After the heavy lectures involving complex functions

Take the length of the words and you get the first 15 digits of PI. Here is the proof:

import sys import math s = """ How I want a drink alcoholic of course After the heavy lectures involving complex functions """ print [len(w) for w in s.split()] print math.pi

Output:

[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9] 3.14159265359 # the last digit is rounded here

Read more on PI at http://www.eveandersson.com/pi/.

And if you didn’t know, here is the PI song :)

**Update (20110317)**

You can approximate the value of PI with 355/113. The first 6 decimal places are the same. It’s quite easy to memorize it: visualize 113355, split into two (113 and 355), then do the division.

>>> import math >>> math.pi3.1415926535897931 >>> 355/113.3.1415929203539825

**Ref.:** Kee Nethery at python-list.