The Math Forum

Ask Dr. Math - Questions and Answers from our Archives
Associated Topics || Dr. Math Home || Search Dr. Math

Program to Find N-digits of Pi

Date: 9/11/96 at 11:4:15
From: Anonymous
Subject: Program to Find N-digits of Pi

Do you know where I can find C code for doing N-digit decimal 
arithmetic in C?

I want to encode Euler's iterative formula for approximating pi,

sum(1/n^2) -> (pi^2)/6 as n -> inf

But if I use unsigned longs, the max n is 65536 because 65536^2 ==
2^32.  65536 terms gives you a pretty fair approximation (6 digits 
worth, or so), but it gets that in about a decisecond on my pentium-
120; I want an iterative algorithm I can run for DAYS.  So obviously 
I'll need to store my numbers in strings, limited only by my RAM.

I've written N-digit arithmetic, (just finished division last night, 
that was tough!) but it only uses whole numbers (division will 
generate a real, but currently can't use reals for the divisor or 
dividend (yet).  I tried it, but since I'm storing the numbers from 
LSB to MSB in the string, I ended up just making a big mess.

If there's code out there that already does this, I'd love to get my 
hands on it.


Date: 9/12/96 at 0:39:3
From: Doctor Pete
Subject: Re: Program to Find N-digits of Pi

I know this isn't quite what you're asking for, but there are series
expressions that converge to pi *much* faster than the one you gave. 
Similar to yours, the formula

     Sum[1/n^4,{n,1,Infinity}] = Pi^4/90

is also true, and is slightly faster.  Incidentally, there is an 
infinite family of such series; i.e.,

     Sum[1/n^(2k),{n,1,Infinity}] = Pi^(2k)*P/Q,

for positive integers P, Q, which derives from the properties of the 
Riemann zeta function.

Even faster are series based on arctangent formulae, such as

      -Sum[(-1)^n/((2n+1)*239^(2n+1)),{n,0,Infinity}] = Pi/4.

Incredibly fast are some series by Ramanujan; i.e.,

       {n,0,Infinity}] = 1/Pi .

Regarding your question, there do exist arbitrary precision libraries, 
I believe, on the net.  Try a web search.

If you want to know more about the series expressions I mentioned 
here, as well as additional formulae, visit the following site:   

-Doctor Pete,  The Math Forum
 Check out our web site!   
Associated Topics:
High School Calculators, Computers

Search the Dr. Math Library:

Find items containing (put spaces between keywords):
Click only once for faster results:

[ Choose "whole words" when searching for a word like age.]

all keywords, in any order at least one, that exact phrase
parts of words whole words

Submit your own question to Dr. Math

[Privacy Policy] [Terms of Use]

Math Forum Home || Math Library || Quick Reference || Math Forum Search

Ask Dr. MathTM
© 1994- The Math Forum at NCTM. All rights reserved.