My father, Dik T. Winter, once wrote the following program in C to compute 800 digits of pi:
int a=10000,b,c=2800,d,e,f[2801],g;main(){for(;b-c;)f[b++]=a/5;
for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a)for(b=c;d+=f[b]*a,
f[b]=d%--g,d/=g--,--b;d*=b);}
This version is featured on the website of the Applied Cryptography Group of Stanford University.
I came up with the following Haskell code, which is not limited in the number of digits it computes:
import System.Environment
pidigits _ _ _ _ 0 = []
pidigits j n a d num =
if
o > b || r + o > e
then
pidigits (j + 1) o b e num
else
q : pidigits (j + 1) (o * 10) ((b - q * e) * 10) e (num - 1)
where
o = n * (j + 1)
b = (a + n * 2) * k
e = d * k
(q, r) = divMod (o * 3 + b) e
k = j * 2 + 3
main = getArgs >>= (putStrLn . showpidigits . read . flip (!!) 0)
showpidigits = ("3." ++) . concat . map show . tail . pidigits 0 1 0 1
Source: pidigits.hs