# Extra Credit 1: Making PI

## Due: Friday May 11, 2012

*Note:* This extra credit assignment is worth 2 points (same as
homework). Recall that extra credit can only boost your homework
and classwork scores and not your quiz and final exam scores.
Also, extra credit will only be given for submissions that are mostly
correct. For example, a submission that would receive 5/10 on homework
would not receive any points for extra credit.
Please consult the syllabus:
Syllabus: Extra Credit.

## Objectives

More practice with loops.

## The Assignment

### Part 1: Leibniz & Gregory

Your assignment is to estimate the value of π (=3.14159...) using two different formulas. The first formula, typically attributed to James Gregory (1638 – 1675), and Gottfried Leibniz (1646 – 1716) looks like this:

Notice that the denominators are the odd numbers. If an infinite number
of terms is included, then the sum will indeed equal π. However, we
can't compute the exact value of π this way because it will literally
take forever. Nevertheless, we can still use the Gregory-Leibniz formula
to *estimate* the value of π by including only finite number
of terms.

The first part of your assignment is to write a program that
uses the Gregory-Leibniz formula to estimate π. Your program
should first ask the user how many terms she wants to include. Let's
call this number *n*. Then your program should report the
estimated value of π using the first *n* terms of the formula.

For example, if the user says *n* is 1, then your program uses
the estimate that:

(This is a very bad estimate.)

If the user says *n* is 2, then your program uses the estimate that

Similarly, for *n* = 3, your program uses:

Note that you will need to use a loop, since you do not know what the
user will specify for the value of *n*. Your program should
printout the current estimate for the value of π in each iteration of
the loop. A sample run of your program might look like:

PT[104]% ./a.out Number of terms? 10 1: 4.00000000000000000000 2: 2.66666666666666696273 3: 3.46666666666666678509 4: 2.89523809523809561028 5: 3.33968253968254025210 6: 2.97604617604617649462 7: 3.28373848373848442606 8: 3.01707181707181781860 9: 3.25236593471887669438 10: 3.04183961892940324390 pi is approximately 3.04183961892940324390.

When you are done coding and debugging, play around with your program.
How many terms of the Gregory-Leibniz formula is needed for the estimate
of π to be within 5 digits of accuracy after the decimal point?
(I.e., when would your program report that π is approximately
3.14159...?) Put your answer in a file called
`ec1_answers.txt`.

### Part 2: Bailey & Borwein & Plouffe

For Part 2 of this assignment, you will use a more complicated formula for π due to Bailey, Borwein and Plouffe (1997):

Notice that the values of the denominators go up by 8. In the first group, the denominators are 1, 4, 5 and 6. If you add 8 to each of these numbers, then you get 9, 12, 13 and 14, which are the denominators in the second group.

Write a program that prints out the approximation of π using the BBP formula. Your program should first ask the user for the number of "groups" to include. When the user enters 1, 2 and 3 respectively, your program should use the following estimates of π:

How many groups have to be included in the BBP formula in order to get
π to 5 digits of accuracy? (Place your answer in
`ec1_answers.txt`.)
What happens when the user asks for 100 groups?

## Notes

- Use a format like
`%1.20f`to print out all the digits of your estimate for π. - For part 2, you should not need to use the
`pow()`function to compute (1/16)^{i}. Just initialize a double variable with the value 1.0. Then, each time through the loop, divited this coefficient by 16.

## What to Submit

Make sure that you have answered the 3 questions in:
`ec1_answers.txt`.

Use the `script` command to record 3 sample runs of each of your
two programs using a different number of terms/groups.
Then, submit your programs, sample runs and answers using:

submit cs104_chang ec1 pi1.c pi2.c typescript ec1_answers.txt