Drexel dragonThe Math ForumDonate to the Math Forum

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

Finding Pi in C++


Date: 10/10/2001 at 19:24:38
From: Chris
Subject: Finding PI in C++

Hi,

I have to write a program in C++ to find the first 100 digits in Pi.  
I'm supposed to follow the sequence: 4+(4/3)-(4/5)+(4/7)-(4/9)... etc.  
I have no idea where to begin except to use a WHILE or FOR loop. Any 
ideas?

Thanks.
Chris


Date: 10/11/2001 at 09:51:24
From: Doctor Paul
Subject: Re: Finding PI in C++

First, I think you have the addition and subtraction backward:

Pi = 4 - (4/3) + (4/5) - (4/7) + ...

I've never seen this formula for Pi before, but it is nonetheless 
true. I was convinced that your formula wasn't true because as I 
summed the series, I didn't get anything close to Pi.

I'm not familiar with C++, but I'm assuming it has the value of Pi to 
100 decimal places built in (or you can find it). If not, I'm not sure 
how you would be expected to do the assignment.

All you need to do is compare the value of the sum of the series at 
any given term to the known value of Pi. For example:

If the difference between the two is less than .01, then you are 
guaranteed *one* (not two) digit of accuracy. So if you want 2 digits 
of accuracy, continue summing terms of the series until the difference 
is less than .001.  And so forth.

Something kind of interesting occurs: I told Maple to sum the terms of 
the series as long as the difference between the values was > .01

Maple did this and it stopped after the 100th term. So after the 100th 
term in the series, the sum was < .01 away from the actual value of 
Pi.

Then I told Maple to sum the terms of the series as long as the 
difference between the values was > .001

Maple did this and it stopped after the 1000th term. So after the 
1000th term in the series, the sum was < .001 away from the actual 
value of Pi.

So after the 1000th term I was guaranteed 2 digits of accuracy. 
Two digits of accuracy actually came much earlier in the series, but 
there was no guarantee until the difference between the two number 
was < .001.

It seems that if I want to guarantee that the two numbers will be less 
than 10(-n) apart, I need to have the program sum 10^n terms in the 
series.

If this is the case, then this series is converging to Pi very slowly.

So I wonder if you would have to compute 10^101 terms in the series to 
guarantee 100 digits of accuracy. Maybe I'm not doing something 
correctly here because 10^101 is a VERY large number. I doubt your 
program could compute that many iterations in your lifetime. Even if 
your computer could compute 1 million terms a second (mine does about 
10 per second), your program would take 3.16 x 10^87 years to run.

Finally, here's the program I wrote. Yours will have to be different 
because the syntax won't be the same. But this should give you an 
idea.

> restart;
> i:=2;

                                i := 2

> t:=1;

                                t := 1

> pie:=0;

                               pie := 0

> count:=0;

                              count := 0

> while evalf(abs(pie-Pi)) > .01 do
> pie := pie + (-1)^i * 4/t:
> t:= t + 2:
> i:=i + 1:
> print(evalf(pie)):
> count:=count + 1:
> od:

the variable pie keeps track of the sum of the series. The (-1)^i term 
makes it add and subtract with alternating terms. And the variable 
count isn't necessary - it just keeps track of how many terms the 
series has to sum before it reaches the desired level of accuracy.

If you need more, there are *many* more formulas for Pi at Eric
Weisstein's World of Mathematics:

  http://mathworld.wolfram.com/PiFormulas.html   

I'm sure some of them can be implemented into a programming language.

I hope this is helpful. Please write back and let me know how it turns 
out. I'm curious now. I'd especially like to know if you can confirm 
or deny my conjecture that it takes 10^(n+1) iterations to guarantee n 
digits of accuracy.

- Doctor Paul, The Math Forum
  http://mathforum.org/dr.math/   
    
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-2013 The Math Forum
http://mathforum.org/dr.math/