Xref: utzoo comp.compression:229 sci.math:16386 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!elroy.jpl.nasa.gov!decwrl!pa.dec.com!rust.zso.dec.com!shlump.nac.dec.com!ryn.mro4.dec.com!allvax.enet.dec.com!jroth From: jroth@allvax.enet.dec.com (Jim Roth) Newsgroups: comp.compression,sci.math Subject: Re: A program for computing Pi Message-ID: <4277@ryn.mro4.dec.com> Date: 3 Apr 91 16:08:31 GMT Sender: guest@ryn.mro4.dec.com Followup-To: comp.compression Organization: Digital Equipment Corporation Lines: 50 In article <1991Apr3.070614.11682@agate.berkeley.edu>, greg@garnet.berkeley.edu (Greg Kuperberg) writes... >In article <1991Apr3.014832.15021@linus.mitre.org> bs@faron.mitre.org (Robert D. Silverman) writes: >>In article avenger@wpi.WPI.EDU (Samuel Joseph Pullara) writes: >>>Could someone please send me a program that will calculate PI to any >>>arbitrary number of digits? Thanks in advance... >>Let me add my opinion. >>Please do NOT comply with the above request. > >That's a great idea! Here is a simple C program to compute n digits of >Pi in time O(n^2). Here's an even shorter program :-) Stan and I once sat down and wrote a one line TECO macro that did this calculation! /* 1000 digits of PI */ /* 'Spigot' algorithm origionally due to Stanly Rabinowitz */ #include stdio #include math main() { int d = 4, r = 10000, n = 251, m = 3.322*n*d; int i, j, k, q; static int a[3340]; for (i = 0; i <= m; i++) a[i] = 2; a[m] = 4; for (i = 1; i <= n; i++) { q = 0; for (k = m; k > 0; k--) { a[k] = a[k]*r+q; q = a[k]/(2*k+1); a[k] -= (2*k+1)*q; q *= k; } a[0] = a[0]*r+q; q = a[0]/r; a[0] -= q*r; printf("%04d%s", q, i & 7 ? " " : "\n"); } } >Enjoy the program! - Jim