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

double f(double x)
{
    return (4.0 / (1.0 + x*x));
}


int main(int argc, char *argv[])
{   int myid, numprocs;
    double width, val, pi;
	
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);
		
    width = 1.0 / (double) numprocs;  /* number of intervals */
    val = width * f( ((double) myid + 0.5) * width);
			
    MPI_Reduce(&val, &pi, 1, MPI_DOUBLE, MPI_SUM, 0, MPI_COMM_WORLD);
			
    if (myid == 0) printf("Approx %.10f\n", pi);

    MPI_Finalize();
    return 0;
}
