/* * 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. */ package csci3366sample.numint; public class NumIntSeq { /* constants */ private static final int NUM_STEPS = 400000000; private static final double step = 1.0/(double) NUM_STEPS; /* global variable -- global so in the parallel version all threads will * have easy access */ private static double sum = 0.0; /* main method */ public static void main(String[] args) { /* start timing */ long startTime = System.currentTimeMillis(); /* do computation */ for (int i=0; i < NUM_STEPS; ++i) { double x = (i+0.5)*step; 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 with " + NUM_STEPS + " steps"); System.out.println("computed pi = " + pi); System.out.println("difference between estimated pi and Math.PI = " + Math.abs(pi - Math.PI)); System.out.println("time to compute = " + (double) (endTime - startTime) / 1000); } }