[ create a new paste ] login | about

Link: http://codepad.org/rYEGMjhs    [ raw code | output | fork ]

mohit_at_codepad - C, pasted on Apr 26:
/* Print the value of pi */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define DECIMAL_ACCURACY 1E-6

/**
 * Using Basel problem (sum of series of inverted square)
 * http://en.wikipedia.org/wiki/Basel_problem
 */

int main() {
  double fac = 0.0;
  double val = 0.0;
  int x = 0;
  do {
    ++x;
    fac = 1.0 / x / x;
    val += fac;
  } while(fac > DECIMAL_ACCURACY);
  printf( "Value of pi is: %g\n", sqrt(6.0 * val) );
  return 0;
}


Output:
1
Value of pi is: 3.14064


Create a new paste based on this one


Comments: