Review studio procedures before starting.

Feel free to participate in a different group than last time. This is totally up to you, but try to find a group that makes it easy for you to participate.

By the end of this session, a TA will have completed a Studio Observation form for your group. You will have to attach your team's observations so keep track and type things in as you go along for printing by the end of studio.

Be careful how you use the web. You are required to develop solutions as a group by thinking not by finding solutions that have been thought out by others. You must be able to explain anything that you have done.

Start with this problem:

Your group is auditioning for Lost by proving your group's ability to compute Pi using only the materials at hand, as follows:

  1. As a group, develop an approach for computing Pi based on the above materials.
  2. Implement your approach using iteration. You can start with the following file that you can paste into a new Java class in one of your lab projects.

    You will need to simulate a random dart thrower. The function math.random() will help, as it returns a nonnegative double less than 1.0. You may also find the Math.sqrt() function to be helpful.

  3. Study and discuss how well your technique computes Pi.

Now try this problem:

Fill in the following iterative method for computing the sum of the square of the first n positive integers:

  // do this on paper, no need to code this

  int sumSquared(int n) {

     // Initialization

     int ans =  ??,
          i   = ??;

     while ( i != n  )   {
        //  place A
        i = i + 1;
        ans = ans + ??
        //  place B

     return ans;
  1. What is the termination condition for the loop?
  2. What loop invariant will help you prove that the loop computes
             ans == 1*1 + 2*2 + 3*3 + ... + n*n ?
  3. Complete the initialization so your loop invariant holds before the loop executes.
  4. Write a proof that the loop body preserves the loop invariant. You want to show the following:
    if prior to the loop body (place A), the following holds:
         ans == 1*1 + 2*2 + ... +  i*i
    then at place B the following holds:
         ans' == 1*1 + 2*2 + ... +  i'*i'
    where ans' and i' are the new values computed by the loop body.

If you have time, pick one or both of the following:

  1. Investigate the fairness of the math.random() method.
    1. What normative criteria should a random number possess?
    2. How can you measure the fairness of a random number generator?
    3. Implement some tests and discuss your results amongst yourselves and other groups.
  2. There are other ways of computing Pi. Try some of these and study their effectiveness in terms of the number of iterations you use.

Last modified 06:49:44 CDT 16 September 2008 by Ron K. Cytron