# CSCI 150: PreLab 2

The Basics
Due: 9AM on Wednesday, February 7 (in King 223 Conference Room)

In this prelab you will familiarize yourself with some of the mathematical and design questions underlying Lab 02 by working on the first three steps of the programming process (describing and understanding the problem and designing an algorithm). There are 10 questions to answer which appear in boxes below.

Please type your solutions and hand in a printed paper copy before the beginning of class on Wednesday. Remember, no late prelabs allowed!

Each prelab will include a short non-technical reading assignment. The first of these comes from a coding bootcamp and deals with expectations about behavior and etiquette in the lab. Our department is dedicated to fostering a warm, encouraging, and supportive student community. This reading is meant to help (a) lay out some basic ground rules that help build a welcoming community and (b) draw attention to some of the phenomena (e.g. Imposter Syndrome) that often make people feel isolated or inadequate.

Read the Recurse Center's guide to the environment.

1. Did you do the reading? (Yes or no will suffice.)

## Loop Practice

Write down the output generated by the following two chunks of code. Remember that the print method generates a new line after printing the string it is passed, unless the "end" parameter sets it otherwise.

2.
``````
for i in range(1, 5) :
for j in range(1, 2*i+1) :
print("*", end="")
print("!")
``````
3.
``````
for i in range(4,0,-1) :
for j in range(1, i+1) :
print(i, end="")
for j in range(1, i+1) :
print(j, end="")
print("?")
``````

## Loopin'

In this portion of the lab you'll start thinking about how computers can handle repetition.

### Fibonacci

The Fibonacci numbers are a sequence of integers defined by the following recurrence relation

 Describe the Problem: The problem you will solve on your lab is as follows. Input: get a positive integer n from the user. Goal: compute and output the n-th Fibonacci number. Understand the Problem: For example, the first six Fibonacci numbers are 1, 1, 2, 3, 5, 8.

4. Compute the next eight Fibonacci numbers.

Hint: you should end with 377.

### Interesting

In light of the recent economic meltdown, you've decided to take a more conservative approach with your financial portolio; as of today, you're getting rid of all your sub-prime morgage securities, all your toxic assets (what were you thinking?), and your considerable stockpile of pork bellies.

To play it safe, you're going to put your money into a savings account. This account earns a fixed percentage of interest every month. Assuming you make reguar, monthly deposits as well, you'd like to know how much you'll have after a given number of month, assuming your monthly contribution occurs after interest is accrued.

 Describe the Problem: The problem you will solve on your lab is as follows. Input: get an initial deposit, a monthly interest rate, a monthly deposit, and a number of months n from the user. Goal: compute and output the total money in your possession at the end of n months. Understand the Problem: For example, suppose your initial savings is \$100, the monthly interest rate is 1%, and you plan on contributing \$20 a month. Then during the first month, you'd get \$1 of interest (100 * 0.01) followed by another \$20 from your regular contribution. So you'd end the first month with \$121.On the second month, your interest would be \$1.21 (i.e., 121 * 0.01), which, together with you fixed \$20, leaves you with \$142.21.After the third month, you'd have \$163.63. Technically, the amount you'd have on month 3 would be \$163.6321, but most accounts don't keep track of fractions of pennies (as fans of the movie Office Space may know), so you'll want to cut off anything below 2 decimal places.
5. Calculate the expected balance after months 4 and 5. Feel free to use a calculator, but make sure your answers seem plausible.

## Monte Carlo!

You probably remember bumping into that peculiar number π = 3.14159265..., right? It comes up when you're talking about circles and trigonometry, but also appears in a bunch unexpected places that seem to have little to do with either. As a refresher, Pi can be defined as the ratio of a circle's circumference (π d) to its diameter (d). One interesting feature of Pi is that it's an irrational number, meaning it cannot be expressed as a fraction m/n where both m and n are integers; consequently, its decimal representation never ends or even repeats. In fact, we can make a stronger statement about Pi: it is a transcendental number, which means that there is no polynomial with rational coefficients of which Pi is a root. But, I digress.

Since ancient times, mathematicians have been fascinated with the study of Pi and it's properties. Early approximations of Pi, such as 22/7 and 355/113 were accurate to 3 and 7 digits repsectively (the latter approximation was the best known for nearly a millenium). Currently, more than the first trillion (a million million) digits are known. There are many ways to estimate Pi -- for example, you could draw as precise a circle as you can manage, measure its circumference C and diameter d, and then divide C/d; this should give you Pi. Alternatively, there is a geometry-based approach due to Archimedes. We'll investigate a third approach using what is called a Monte Carlo method.

### Monte Carlo Method

When we say we're using a Monte Carlo method, we usually mean we're going to do a bunch of random trials and observe the fraction of those trials that have a certain property. In our case, we're going to be throwing darts into a square region, and computing the fraction of those darts that land within a circle inscribed inside that square. Each throw is a trial, and the property we are concerned with is whether or not the dart landed inside the circle or not.

 Describe the Problem: The problem you will solve on your lab is as follows. Input: get a number of trials n from the user. Goal: compute and output an approximation to Pi using a Monte Carlo method with n trials. Understand the Problem: More precisely, we'll begin by (theoretically) constructing a target circle inscribed in a square. This is our dart board, and the target circle reaches all the way to the edge of the square. It might look something like the following: Next, we'll simulate repeatedly throwing darts at random against the board above (we'll assume that our random throws alway hits the square, and are equally likely to land at any given point inside the square). We then look at the fraction of the darts that land within the circle out of all those that were thrown. I claim that if we then multiply this fraction by 4, we should have a good approximation to Pi. What sort of dark sorcery is this? Let's take a closer look. Suppose you are throwing darts at the square board. Assuming it lands on the board, the chance that any one dart is inside the circle is the ratio of the area of the circle to the area of the square. So, if the circle had area 90 and the square area 100 (not possible, but humor me), you'd have a 90% chance of hitting the circle.
6. Suppose the circle has a radius of r.
• What is the length of one side of the square board in terms of r?
• What is the area of the square board in terms of r?
• What is the area of the inscribed circle in terms of r?
• Use these to determine the probability that a randomly thrown dart lands within the circle. That is, determine the ratio R of the two areas (the circle's area divided by that of the square). Simplify your expression for R (it will have a Pi in it). Then give an expression for Pi in terms of R using (simple) algebraic manipulation.
7. On an unrelated note, suppose you have a random number generator that generates a random number x between 0.0 and 1.0 (inclusive). How could you use this restricted generator to generate a random number between -1.0 and 1.0? That is, give a purely arithmetic expression in terms of x such that if x is a random number uniformly distributed between 0.0 and 1.0, then your expression should be a random number uniformly distributed between -1.0 and 1.0.

## Part 3 - Patterns, Patterns Everywhere

In the next batch of problems, we're going to be looking for patterns in sequences of figures. For each problem, a few figures are given, each of which made out of ASCII characters and is associated with an index (a number).

Describe the Problem: The problem you will solve on your lab is as follows.

Input: get a positive integer n indicating an index in a sequence of patterns.
Output: print the n-th pattern to the console, for whichever pattern you are currently on.
Understand the Problem:

For example, below are some indices (the numbers on the left) and their associated figures for Pattern A.

### Pattern A

1. ```  1 2 3
1 2 3
1 2 3
```
2. ```  1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
```
3. ```  1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
```
You can probably see what's happening; figure 3 consists of 3 rows, each of which includes all numbers from 1 to 3. You can infer that figure 2 would look like

1. ```  1 2
1 2
```
and figure 6 would look like

1. ```  1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
```
Line 1 of figure 6 is all numbers from 1 to 6. Line 2 of figure 6 is also all numbers from 1 to 6. In general, line i of figure 6 is all numbers from 1 to 6. Even more generally, we can give the following characterization of Pattern A:

Line i of figure n consists of all numbers from 1 to n.
Design an Algorithm: In order to design an algorithm for this problem, we need to formulate a precise description of what the current pattern is. In particular, an algorithm to outputpattern A is as follows:

• Get the index n from the user.
• For each i from 1 to n
• print out line i of figure n, which consists of all numbers from 1 to n

Below we will give you some different patterns. Your task is to (1) show that you understand each pattern, by giving the next pattern in the sequence, and (2) design an algorithm for the problem, by defining what an arbitrary line i of figure n would contain. Note that in the example above, the description of line i in figure n did not depend on i. In the following patterns, the description of a line may depend on i, n, or both.

### Pattern B

1. ```  1 1 1
2 2 2
3 3 3
```
2. ```  1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
```
3. ```  1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
5 5 5 5 5
```
8. What is the 6th figure in Pattern B?
9. Describe line i of figure n in Pattern B, i.e. "Line i of figure n consists of ....".

### Pattern C

1. ```  1 2 3
2 3
3
```
2. ```  1 2 3 4
2 3 4
3 4
4
```
3. ```  1 2 3 4 5
2 3 4 5
3 4 5
4 5
5
```
10. Describe line i of figure n in Pattern C. Be sure to check that your description works for a range of values of both i and n.

## Honor Code

If you followed the Honor Code in this assignment, write the following sentence attesting to the fact at the top of your prelab.

I affirm that I have adhered to the Honor Code in this assignment.