/* Program to approximate Pi via the power series
      sqr(pi) = 6 + 6/(2*2) + 6/(3*3) + ...
*/

#include <stdio.h>
#include <math.h>

int main () {
  int trial;
  int number_terms = 1000000;
  int index;
  double sum_up, sum_down, i_real;

  /* print headings */
  printf ("    Number of             Approximations to Pi\n");
  printf ("      Terms       Biggest First     Smallest First\n");
  for (trial = 1; trial <= 12; trial++) {

    
    /* compute terms in ascending order */
    sum_up = 0.0;
    for (index = 1; index <= number_terms; index++) {
      i_real = index; /* convert i to real */
      sum_up += 6 / (i_real * i_real);
    }

    /* compute terms in descending order */
    sum_down = 0.0;
    for (index = number_terms; index >= 1; index--) {
      i_real = index; /* convert i to real */
      sum_down += 6 / (i_real * i_real);
    }
      
    /* print results */
    printf ("%11d  %17.10lf  %17.10lf\n", number_terms, sqrt(sum_up), sqrt(sum_down));

    /* double number of terms for next time */
    number_terms *= 2;
  }

}

