/* * numerical integration example, as discussed in textbook: * * compute pi by approximating the area under the curve f(x) = 4 / (1 + x*x) * between 0 and 1. * * sequential version using Java. */ public class NumIntSeq { /* constants */ private static final int NUM_STEPS = 1000000000; private static final double step = 1.0/(double) NUM_STEPS; /* main method */ public static void main(String[] args) { /* start timing */ long startTime = System.currentTimeMillis(); /* do computation */ double sum = 0.0; for (int i=0; i < NUM_STEPS; ++i) { double x = (i+0.5)*step; sum = sum + 4.0/(1.0+x*x); } double pi = sum * step; /* end timing and print result */ long endTime = System.currentTimeMillis(); System.out.println("sequential program results"); System.out.println("pi = " + pi); System.out.println("time to compute = " + (double) (endTime - startTime) / 1000); } }