Calvin seal CS 108: Introduction to Computing
Spring 2006

Project #5

The Project

Prepare for the project by following the general instructions for Project #5.

I've always felt very bad for William Shanks (as described in our videos) who spent over twenty years of his life trying to compute pi to 707 decimal places, only to make a mistake on digit 528. It's especially tragic because our computers can compute pi correctly to the same number of digits in a heartbeat.

So the goal of this project is to emphasize the tragedy of William Shanks.

The Setup

The purpose of your program is to experiment with pi computations. So getting the right answer is less important than discovering interesting properties about the computations. Consequently, you will have no unit tests for this assignment, but instead you will run a driver several times to discover the interesting properties.

Start with these methods in PiCalculationsCLIDriver:

public static void main(String[] args) {
    Screen theScreen = new Screen();
    Keyboard theKeyboard = new Keyboard();
    theScreen.print("Enter the number of iterations: ");
    int iterations = theKeyboard.readInt();
    PiCalculationsCLIDriver.report("Indiana-State-Senate technique",
            PiCalculations.indiana(iterations));
    // repeat the previous line, once for each method in PiCalculations
}

private static void report(String techniqueName, BigDecimal valueForPi) {
    Screen theScreen = new Screen();
    theScreen.println(techniqueName + " reports " + valueForPi
            + " as the value for pi.");
}

You will also need this method in PiCalculations:

public static BigDecimal indiana(int iterations) {
    return new BigDecimal("4.0");
}

The BigDecimal is a standard data type in Java. It can store decimal numbers to any arbitrary (but finite) precision.

Modify this code in the following ways:

  1. Change PiCalculationsCLIDriver#report() so that it also displays the difference between valueForPi and the actual value of pi. Also, indicate if the difference is less than 10-707.
    1. The Wikipedia article on Pi has pi to 1000 digits. Make a constant BigDecimal set equal to that value in PiCalculationsCLIDriver.
    2. BigDecimal arithmetic must be done with methods; suppose a and b are BigDecimals:
      • BigDecimal c = a.subtract(b);
        
        Subtracts b from a, and that new value is put into c.
      • a.compareTo(b) will return -1 if a is less than b.
  2. Write three more computations for pi in PiCalculations.
    1. indiana(int) ignores its parameter, but your methods shouldn't. Use that parameter to regulate the number of iterations it makes in computing pi.
    2. As indicated in the code comment, duplicate the report() call, once for each computation method. You can (and should) get rid of the report for the indiana() method.
    3. Pick three techniques to compute pi from the Wikipedia article on Pi (or other sources), and implement them.
    4. The BigDecimal documentation describes a lot of methods you might need or even like to use.
    5. You may get better results if the counting-for loop counts backwards.

Write up an OCD for one of your computations. Indicate a source for each of the computations that you implement in this same design document.

The Experiments

The goal of this project is to find the number of iterations each technique requires to get an accuracy of 707 digits. That is, the absolute value of the difference should be less than 10-707. You have to figure this out for yourself by running the driver over and over again with a different number of iterations each time.

However, it appears that many techniques get nowhere close to this kind of accuracy in Java in a reasonable amount of time. If a particular computation takes longer than five minutes, reduce the number of iterations until you find a number that takes about five minutes.

In your design document (along with your OCD), add a section where you list the techniques you employed. For each technique, report one of two things:

  • Indicate how many iterations and the amount of time it takes to get the desired accuracy, if the technique acheived the desired accuracy.
  • If the computations for the technique take too long to get the desired accuracy, indicate how many iterations you can do in five minutes and how accurate the result is.