There's a simpler way.
Still include math.h
This works because the units are radians by default (2 radians = 360 degrees)Code:double pi = 4.0 * atan( 1.0 );
The arctangent of one is 1/4 pi
Just multiply 1/4 pi by 4 and you get pi ^.^
There's a simpler way.
Still include math.h
This works because the units are radians by default (2 radians = 360 degrees)Code:double pi = 4.0 * atan( 1.0 );
The arctangent of one is 1/4 pi
Just multiply 1/4 pi by 4 and you get pi ^.^
Forum DOs and DON'Ts
Please use CODE tags
Including your email address in a post is not recommended
My Blog
Yeah, there are dozen of way to solve PI, but compare to that method, if you want to get millions or billions of decimals, it's kind of slow. Because calculate atan itself may use Taylor expansion. its rate of convergence can not be as fast as that method. This is why it attracts me to try to find information about it.
Pn = An - Bn = ( sqrt(An) + sqrt(Bn) )( sqrt(An) - sqrt(Bn) )
Pn+1 = An+1 - Bn+1 = (An + Bn)/2 - sqrt ( An*Bn ) = 1/2 * ( sqrt(An) - sqrt(Bn) )^2
When n->infinite, let's find
lim (Pn+1)/ (Pn)^2 = lim 1/2 * 1 / ( sqrt(An) + sqrt(Bn) )^2 = lim (1/2) * 1/ 4*M (a, b) = 1/(8 M ( a, b ))
So that it becomes a constant,
Pn+1 ~ C* (Pn)^2
Therefore, every iteration could produce double precision of decimal then last time ! which is really fast !
Oh, I may forget it should use sqrt, which uses Newton's iterating theorem, it's slow too when the number of decimal has become skyrocketing, though it has double convergence. Maybe there are some other ways to speed up sqrt, but I don't know about that ...
Fast Fourier Transformation can be used, I could not understand the solution but it is available on internet. This problem is a good place to test how fast the convergence is and how many digits can be computed accurately within time limits.
http://www.spoj.pl/problems/PIVAL/
And as far as I know, many of the best solutions are implementations of FFT.
Thanks for your post, but I don't think I can write a program that can compute millions of digits of PI, I think it's hard, will it ?
I just know how to write a c program without making syntax error for the time being ...
One of the easier problems holding you back from many digits will be floating-point precision. I think IEEE 754 only gives you about 50 bits of precison for 64-bit doubles, which is about 15 decimal digits. if you're using C99, you can get just over twice as many digits out of a long double.
Check out GMP, which allows you to use arbitrary-precision numbers in C
http://gmplib.org/
I know this isn't the main question you're trying to answer, but this might be useful as you move forward.
Thank you !
Bookmarks