Posts‎ > ‎

### Calculating the Pi constant

posted 10 Jul 2011, 14:55 by Rodrigo Caetano Rocha   [ updated 27 May 2015, 20:56 ]
 The mathematical constant π (Greek pi) is commonly used in mathematics. It is also known as Archimedes' constant. π is an irrational number, which means that its value cannot be expressed exactly as a fraction m/n, where m and n are integers. Consequently, its decimal representation never ends or repeats. This post shows a program that calculates an estimation of the π constant. This approach is based on the attached file Pi as an integral. The advantage of this approach is that it calculates the π with a summation, it means that the task can be easily divided among computers so that the final result, that is the π estimation, will be the sum of the result of each computer. The following program is a naive implementation of the method shown in Pi as an integral.  ``` #include #include #include main() {    long double pi = 0.0;    long long int n = 9999999999;    long long int i;    for( i = 1; i<=n; i++ ){       pi += sqrt(               (long double)1.0 -               pow(                  ( (long double)(2.0*i)/n - (long double)1.0 )                  , 2               )             )*((long double)2.0/n);    } ``````    pi *= (long double)2.0;     printf("%.20Lf\n", pi); } ``` Note that the greater is n the more precise will be the estimation. For this example the value calculated for π was 3.141592653589788512... . by rcor
Ċ
Rodrigo Caetano Rocha,
10 Jul 2011, 15:45