/* =========================================================== compute Pi by integrating the function 1/SQRT(1-x^2) =========================================================== */ #include #include #include double f(double a) { return( 2.0 / sqrt(1 - a*a) ); } /* ======================= MAIN ======================= */ int main(int argc, char *argv[]) { int i; int N; double pi; double w, x; /* ----- Check command line ----- */ if ( argc != 2 ) { printf("Usage: %s Num_Intervals\n", argv[0]); exit(1); } /* ----- Get number of intervals and number of threads ----- */ N = atoi(argv[1]); w = 1.0/(double) N; pi = 0.0; for (i = 0; i < N; i = i + 1) { x = (i + 0.5)*w; pi = pi + w*f(x); } printf("Computed Pi = %lf\n\n", pi); }