Name: ______K E Y Student ID: __________________
In this part of the exam you will be designing and coding a Python program.
The sections of the exam correspond to the sections of a lab writeup.

## Problem Statement:

One (very slow) way to calculate the value of pi is to compare the area of the unit quarter-circle with the area of the unit square. We can estimate this by generating random points in the unit square, and counting how many lie within the quarter-circle. The ratio should approximate pi/4 as the number of points increases.

Write a function circle_pi(count, freq) that uses count points to estimate pi, printing out estimates every freq iterations (to show its progress).

Write a testbed program that calls your function, but saves the printed output to a file instead of printing to the screen.

## Problem Suitability: [2]

The heavy computational load of a large number of repetitive calculations is particularly well-suited to a computer program, especially when run on a fast CPU. For the more general task of calculating pi, however, other algorithms may be faster. This algorithm could also be done by hand (e.g., by tossing coins onto a piece of paper), but it would be very slow.

## Problem Restatement: [3]

• Given: count, the number of iterations, and freq, the frequency with which status updates are printed.
• To Do: Estimate pi by generating random points in the unit square, and counting what fraction of those points lie in the unit circle.
• Desired Result: Return the estimate of pi, and print estimates along the way.

## Libraries: [2]

math.sqrt, random.random, sys.stdout

## Problem Refinement (English): [5]

Each time we generate a new random point, find its distance from the origin. If its distance is less than one, it lies within the unit circle, so we increment a counter. Our current estimate of pi is four times the ratio of the number of points in the unit circle to the total number of points we've generated thus far.

Every freq points, we print our current estimate. When we're done, we return our current estimate of pi.

## Data Tables: [3]

### Variables:

incircle (count of how many points lie in the circle), and i (how many points total thus far).

## Sample I/O: [2]

```How many iterations should I use to approximate pi? 100000
How often would you like updates? 10000
Approximating pi....
3.1144
3.1306
3.12746666667
3.1259
3.13176
```

## User Manual: [5]

This program estimates the value of pi by counting how many randomly generated points in the unit square lie inside the unit quarter-circle. This is like throwing darts at a square board and counting how many lie inside the quarter-circle. As the user, you get to choose how many darts to throw. Since accurate results require throwing a lot of darts, you can ask for status updates (the current estimate).

The program will prompt you for the number of darts to throw:

```How many iterations should I use to approximate pi?
```
Type in a positive integer (100,000 is good).

The program will then ask you how often you'd like it to print a progress report:

```How often would you like updates?
```
Type in a positive integer (1,000 is good).

This program then redirects the output to the file "circlepi.log", where you can find the estimates of pi. They should converge (albeit slowly) to the true value of pi.

## Pseudocode: [10]

• Initialize counters incircle=0 and iteration=1.
• While iteration ≤ count:
• generate a new point: (random(), random()).
• If the distance from the point to the origin is less than 1:
• Increment incircle
• Our estimate of pi is 4.0*incircle/iteration
• If iteration modulo freq is zero:
• Print our estimate
• Increment iteration
• Return our estimate of pi

(circlepi.py)