Originally posted by: bobsmith1492
You can just use a simple loop like we did in the easy Programming in C class using a formula like that which was mentioned...
First, you're close... if I recall correctly (and I looked it up to be sure), the algorithm you're looking for is from the formula pi/4 = 1 - 1/3 + 1/5 ...
Yes, you could do a simple loop. However, you need to look at the rate of convergence on an answer. Some algorithms converge FAR faster than other algorithms... that is, for some formulas, you'd have to loop through a million times to get a million digits... for other formulas, you'd only have to loop through a dozen times or so to get a million accurate digits.
According to the site I verified the above formula with, it would take 300 terms (loops) before you calculated Pi correctly to two decimal places!
There are other algorithms that are far more suited to finding the answer - algorithms such that each itteration (loop) doubles the number of correct digits. So, after 1 iteration, there may be 3 correct digits, then 6 then 12, then 24...
And, then there are algorithms that converge even faster.
Some algorithms:
here
Even better, but the site has broken links are
here
If you skim through to about 2/3 of the way down, you'll see that Borweins' quarticall convergent algorithm will give you 2 BILLION correct digits after only 15 "loops"
Actually, that site is pretty informative even without being able to see the formulas being used (in fact, since this is probably an introduction for you to numerical analysis, it's probably less intimidating)
To describe this to you further, I'll give you a simpler example.
Let's find the square root of 2. (the answer is 1.41421356237309504880168872097...)
---------------------------------------------------------------
Method 1:
Use the formula that finds the decimal value after the 1 (add 1 to the final answer)
a(n+1) = 1/(2+a(n)), a(0)=0 (first estimate is 0)
that is, new estimate = 1 divided by the sum of 2 and the previous estimate
Loop 1
a(1) = 1/(2+0) = .5
loop 2
a(2) = 1/(2+.5)= .4
loop 3
a(3) = 1/(2+.4) = .416666666666666666...
loop 4
a(4) = 1/(2+.41666666666...) = .413793103...
loop 5
a(5) = 1/(2+.413793103) = .41428571428571...
loop 6
a(6) = 1/(2 + a(5) ) = .414201183...
loop 7
a(7) = 1/(2 + a(6) ) = .41421568627...
loop 8
a(8) = 1/(2 + a(7) ) = .414213197
Not bad... only 8 loops, and we have 6 accurate decimal places...
on the 16th loop, the answer is .41421356237282141377280856
11 correct significant digits after the decimal
We're gaining almost 1 correct significant digit each time we loop.
---------------------------------------------------------------
Method 2
Also called the babylonian method, and also credited to Newton
The formula is to guess, divide 2 by the guess, average the initial guess with the quotient to find the new guess.. (I'm too lazy to type it symbolically, as if any of the rest of this post shows laziness)
Take a guess. 1.5? (estimate #1)
Okay, 2 divided by 1.5 is 1.3333...
Since 1.5 and 1.33333 aren't equal, the answer must be between them...
So, 2nd "guess" is (1.5 + 1.33333) /2 = 1.41666666... (estimate #2)
Now, 2 divided by 1.416666 = 1.41176470588253529411...
Again, since the two aren't equal, the answer must be between them...
So, guess 3 is the average of the previous 2...
the average of 1.41666... and 1.4117... is 1.4142156862745098039.. (estimate 3)
Let's do it once more.
2 divided by guess 3 = 1.41421143847487001733102253032929
average this with guess 3 and we have a new estimation:
1.41421356237468991062629557889013 (estimate 4)
Now, after 4 "loops" we're accurate to 11 places after the decimal...
----------------------------------------------------------------------------------
Which is faster? 16 loops? or 4 loops? I suppose it depends on the complexity of the calculations in each loop...
It may not seem like much of a time difference now, but (if my numerical analysis skills aren't too rusty) notice that 16 is the square of 4...
The accuracy we'd achieve with 1000 loops using Method 2 would require 1 million loops using method #1. Maybe that still doesn't sound significant... But, how about 1 million loops using method 2... now we're using that computer! If you used method #1, it would require 1,000,000,000,000 (1 TRILLION) loops.
Note that the calculations are slightly more complicated in method 2. For a few loops, or even a hundred loops, perhaps method 1 is faster, well, maybe close to the same speed.
But, let's suppose method 2 took 100 times longer per loop. If we ran it for 1 million loops, it would take one ten-thousandth of the time to run than method 1 for method 1 to have the same level of accuracy.
Now, compare this to what you can read about on the second link I provided above... one of the listed algorithms has 170 correct decimal places after just 3 loops.. and 2 billion decimal places correct after only 15 loops.