how to generate pi in c

Alex Stangl alex at stangl.us
Fri Nov 5 19:25:21 UTC 2010


On Fri, Nov 05, 2010 at 05:40:39PM +0100, Arthur Bela wrote:
> Does anyone has a "generate-pi.c" source code?

Search for pi spigot algorithm.

Here is a tiny C program from Jeremy Gibbon's Unbounded Spigot paper
(due to Dik Winter and Achim Flammenkamp):

a[52514],b,c=52514,d,e,f=1e4,g,h;main(){for(;b=c-=14;h=printf("%04d",
e+d/f))for(e=d%=f;g=--b*2;d/=g)d=d*b+f*(h?a[b]:f/5),a[b]=d%--g;}

This produces the first 15,000 digits concisely, but is obfuscated.
If you need an unbounded number of digits, search out the spigot
algorithm papers.

Alex


More information about the freebsd-questions mailing list