PreLab 03

Prime Egyptian Master Minds
Due by the beginning of lecture on Wednesday, September 14th

In this prelab you will formulate some of the mathematical ideas necessary to complete Lab 03. Please type up your solutions, and hand in a paper copy before the beginning of lecture on Wednesday. Remember, no late prelabs allowed!


Reading

Read Planet Money's article about why there are more men than women in Computer Science.

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

Conditionals

2. Consider the following code snippet:
    if (n%2) == 0 :
        print(n)
    else :
        print(n*2)
        if (n%3) == 0 :
            print(n//3)
What is the output of this code snippet for each of the following values of n?
  • n = 10:
  • n = 9:
  • n = 6:
  • n = 5:

Walk Like and Egyptian

Describe the Problem

In lab you'll be creating a program that draws a pyramid of bricks based on user input.

Input: An integer for the width of the image (width) and the height in bricks of the pyramid (n).

Output: An image of a pyramid that is n bricks tall in a square canvas width wide (and thus width tall).

Understand the Problem

Here are three sample outputs for your reference. Notice that the pyramid doesn't necessarily fill the entire canvas to the right and to the top; if the canvas width is not evenly divisible by the number of bricks, then there will be extra blank space. (A question for you to ponder: why is there so much blank space in the third example? Seems like you could fit lots of extra bricks both to the right and up top...)

400 x 400, 3 bricks.
3 bricks

400 x 400, 17 bricks.
17 bricks

400 x 400, 123 bricks.
123 bricks

3. Suppose you have a canvas that is 7 units wide and 7 units high. It may be useful to imagine that the canvas is made of graph paper, and is 7 squares wide by 7 squares tall. Suppose that you want a pyramid that is 3 bricks high (that is, n=3). You'd like the bricks to be as large as possible, but they should all be of the same size, and they must use a whole number of squares.
  • Draw the canvas and the pyramid to a reasonable scale and with enough precision so that we can see the coordinates and any gaps that might remain between the top (or right side) of the pyramid and the edge of the canvas.
  • What is the side-length of each brick (how wide and tall is each brick)?
  • How many bricks are in the 0th (bottom) row?
  • How many bricks are in the 1st (middle) row?
  • How many bricks are in the 2nd (top) row?
  • Let the upper-left corner of the canvas be (0,0) with the x and y values increasing as you move right and down, respectively. What are the x- and y-coordinates of the upper left corner of the first brick in the 0th row?
  • What are the x- and y-coordinates of the upper left corner of the first brick in the 1st row?
4. More generally, suppose your canvas is width wide and width high, and that your pyramid is n bricks tall. What is the side-length s of a brick, in terms of width and n? Use Python's integer division if necessary.

5. If our pyramid is n bricks tall, then it is also n bricks wide at its base. Thus we have that
  • Row 0 contains n bricks
  • Row 1 (just above row 0) has n-1 bricks
  • Row n-1 (the top row) has 1 brick.
How many bricks does row i contain, in terms of n and i?

6. If our pyramid is n bricks tall and each brick has side-length s, then
  • The first brick of row 0 starts at x-coordinate 0.
  • The first brick of row 1 starts at x-coordinate s//2.
What is the x-coordinate of the first brick of row i, in terms of s and i?

7. If your pyramid is n bricks tall, our canvas width wide and tall, and each brick has side-length s, then
  • The bricks of row 0 have y-coordinate width-s.
  • The bricks of row 1 have y-coordinate width-2s.
What is the y-coordinate of row i, in terms of width, i, and s?

Part 2 - Primes

As you may know, a number x is said to be prime if x is at least 2, and the only proper factors of x are itself and 1. So the first few primes are 2, 3, 5, 7, 11, 13, 17, 19, 23, etc. 4 isn't prime, since it is divisible by 2. Same goes for 6 and 8. 9 is out thanks to 3. And so on. There are a lot of primes. More precisely, there are infinitely many primes. This can actually be shown pretty easily; ask if you're curious.

A twin prime is a pair of prime numbers that differ by exactly 2. So (3,5), (5,7), (11,13), (17,19) and (29, 31) are all twin primes. Note that not every prime is part of a twin prime. It is conjectured that there are infinitely many twin primes too, but no one knows for sure.

Describe the Problem:

The problem you will solve on your lab is as follows.

Input: An integer n from the user that represents the number of primes to print out.

Output: The first n primes, and the number of twin primes amongst these.

Understand the Problem:

If the user enters 13 then the output should be

The first 13 primes are:
2 3 5 7 11 13 17 19 23 29 31 37 41
Amongst these there are 5 twin primes.

Note that (41, 43) is a twin prime, but we didn't count it since 43 wasn't amongst the first 13 primes.

Design an Algorithm:

Write pseudocode to solve this problem. You should decompose your main algorithm into small manageable chunks. For example, you should

8. Suppose x and f are both integers. Find a boolean expression which is true if and only if f is a proper factor of x. That is, the boolean expression should be true if x is evenly divisible by f, and false otherwise. For example, if x and f are 18 and 6 respectively, then the expression should evaluate to true, since 6 goes evenly into 18. For 18 and 5, however, the expression should evaluate to false, since 5 doesn't go evenly into 18.

Use the expression above to give pseudocode for an algorithm that determines whether x is prime.

Part 3 - Mind Mastery

Mastermind is a neat (although oftentimes frustrating) puzzle game. It works a something like this: There are two players. One player is the codemaker (your porgram), the other is the codebreaker (the user). The codemaker chooses a sequence of four colored pegs, out of a possible six colors (red, blue, green, yellow, orange, and purple). He may repeat colors and place them in any order he wishes. This sequence is hidden from the codebreaker. The codebreaker has 10 chances to guess the sequence. The codebreaker places colored pegs down to indicate each of her guesses. After each guess, the codemaker is required to reveal certain information about how close the guess was to the actual hidden sequence.

Describe the Problem:

In this part of the lab, you will create a program to play Mastermind, where computer is playing the codemaker, and the human user is the codebreaker. Thus your program needs to generate a secret code, and repeatedly prompt the user for guesses. For each guess, your program needs to give appropriate feedback (more detail below). The game ends when either the user guesses correctly (wins) or uses up 10 guesses (loses).

Understand the Problem:

The trickiest part of this game is determining how to provide feedback on the codebreaker's guesses. In particular, next to each guess that the codebreaker makes, the codemaker places up to four clue pegs. Each clue peg is either black or white. Each black peg indicates a correct color in a correct spot. Each white peg indicates a correct color in an incorrect spot. No indication is given as to which clue corresponds to which guess.

For example, suppose that the code is RYGY (red yellow green yellow). Then the guess GRGY (green red green yellow) would cause the codemaker to put down 2 black pegs (since guesses 3 and 4 were correct) and 1 white peg (since the red guess was correct, but out of place). Note that no peg was given for guess 1 even though there was a green in the code; this is because that green had already been "counted" (a black peg had been given for that one).

As another example, again using RYGY as our code, the guess YBBB would generate 1 white peg and 0 black; yellow appears twice in the code, but the guess only contains one yellow peg. Likewise, for the guess BRRR, only 1 white peg is given; there is an R in the code, but only one.

Check here for an online graphical version of the game. Note this version uses red instead of black pegs to indicate a correct guess. Also note, this isn't the pinacle of gaming technology.

9. Assuming the code is RYGY, fill in the appropriate number of black and white pegs for each guess.

guess black pegs white pegs
YYYY
YRYR
BBPO
PGYR
YYYG
RYGY

10. Consider the following algorithmic approach for calculating the number of white pegs to be awarded for a given guess.
   set a white counter to 0
   loop through the four positions of the guess
      loop through the four positions of the code
         if the current code character matches the current guess character
         and the guess and code positions are not the same, 
         increment the white counter and exit the inner loop
What goes wrong with this algorithm? Give an example of a code, a guess, the value that should be generated and the value that this pseudocode would generate.

Honor Code

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

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