package edu.rice.hj.example.comp322.labs.lab8; import java.math.BigDecimal; /** * This is the serial version of computing pi that uses fixed number of terms * Original Source Code: http://research.cs.queensu.ca/home/cmpe212/Fall2011/Lab6/Lab6.java */ public class PiSerial1 { public static void main(final String[] args) { final int terms; if (args.length > 0) { terms = Integer.parseInt(args[0]); } else { terms = 1000; } System.out.println("num terms = " + terms); for (int iter = 0; iter < 8; iter++) { final long startTime = System.nanoTime(); BigDecimal sum = BigDecimal.ZERO; // Uses the BBP formula to estimate pi using BigDecimals // http://mathworld.wolfram.com/BBPFormula.html final int scale = 10000; // dummy precision value for (int k = 0; k < terms; k++) { final BigDecimal term = PiUtil.calculateBbpTerm(scale, k); sum = sum.add(term); } final long endTime = System.nanoTime(); final long execTime = (long) ((endTime - startTime) / 1e6); System.out.println("PI = " + sum.toPlainString()); System.out.println("Iteration-" + iter + " Exec Time = " + execTime + " ms."); } } }