The purpose of today's lab is to practice using repetition. There are 2 components:

  1. Finding perfect numbers
  2. Drawing Spirographs

****** Work in pairs, according to this table for section A or this table for section B. ******

Finding Perfect Numbers

A positive integer is called a perfect number if it is equal to the sum of all of its positive divisors, excluding itself. For example, 6 is the first perfect number, because 6 = 3 + 2 + 1. The next is 28 = 14 + 7 + 4 + 2 + 1 . There are four perfect numbers less than 10000. In this first exercise, we will write a program to find these four numbers.

Exercise 5.1a
Our first step will be to write a program called find_perfects.py that determines the positive divisors of a positive integer. Use the following algorithm:
  1. Prompt for a positive integer and store it as value
  2. Initialize low to be 1
  3. Initialize high to be the same as value
  4. Initialize divisors to be an empty list
  5. Repeat as long as low is less than high:
    1. If low is a divisor of value:
      1. Set high to be value integer divided by low
      2. Add low to the list of divisors
      3. Add high to the list of divisors ONLY if it is different from low
    2. Increment low by one

Now that we can effectively determine the positive divisors of an integer, we are ready to tackle the problem of checking if a number is perfect.

Exercise 5.1b
Extend your code from part a to check if value is a perfect number. A few notes: Print a descriptive message to the user indicating if their choice is a perfect number or not. Verify that your code correctly determines that 6 and 28 are perfect numbers, and that 17 and 439 are not.

Since we can tell if a number from the user is perfect, we are ready to tackle the enumeration problem of finding perfect numbers less than 10000.

Exercise 5.1c
Extend your code from part b to find and print the four perfect numbers less than 10000. There are multiple approaches here, but for today, use this one:

Drawing Spirographs

In this exercise, you will use the turtle graphics package to generate pictures like those which can be made with a spirograph set. A Spirograph is formed by rolling a circle inside or outside of another circle. The pen is placed at any point on the rolling circle. See http://en.wikipedia.org/wiki/Spirograph

We will use iterative development to develop a program that allows a user to generate spirograph pictures.

Exercise 5.2a
Begin by creating a file named spirograph.py that implements the following algorithm:
  1. Import the turtle and math libraries.
  2. Create the turtle window and a turtle (refer to a previous lab if you don't remember how to do this).
  3. Prompt for the moving radius and store this float in mov_rad.
  4. Prompt for the fixed radius and store this float in fix_rad.
  5. Prompt for the offset of the pen point and store this float in pen_offset.
  6. Prompt for the color and store this string in color.
  7. Draw the spirograph.
  8. Make it so the spirograph window closes when clicked.

A spirograph picture can be drawn by using these parametric equations:

In these equations, t is the current time, R is the radius of the fixed circle, r is the radius of the moving circle, and p is the offset of the pen point in the moving circle.

Exercise 5.2b

To draw a spirograph, you will need to do two things, both of which will be coded in the space you left in the algorithm specified above. First, you need to get set up and then you need to use a loop to move the turtle over “time”. Do this as follows.

  1. # First, do the setup.
  2. Initialize the current time to 0.0.
  3. Tell your turtle to pick up its pen.
  4. Tell your turtle to move to the appropriate initial position. In particular, use the equations given above to compute x(0) and y(0).
  5. Tell your turtle to put the pen down.
  6. Tell your turtle to set the pen color to the value given by the user.
  7. # Then draw the spirograph.
  8. Loop while the time is less than 100.
    1. Increment time by 0.1.
    2. Compute a value for x using the new value of time. Use the equations specified above.
    3. Compute a value for y using the new value of time.
    4. Tell your turtle to go to the location specified by x and y.

Assuming you have carefully followed the algorithm, you should be able to test your code now. For example, you might try these values:

At this point we are able to draw a nice pretty spirograph. But, the fun really starts when you can draw multiple spirographs on top of each other, in different colors.

We want a run of the code to act like this:

Would you like to draw a spirograph? (Y/n): Y
<4 questions here to get r, R, p, and col.>
<Code draws the spirograph.>

Would you like to draw a spirograph? (Y/n): <user hits enter, which defaults to Y>
<4 questions here to get r, R, p, and col.>
<Code draws the spirograph.>

Would you like to draw a spirograph? (Y/n): Hello!
Please enter Y or n.
Would you like to draw a spirograph? (Y/n): y
<4 questions here to get r, R, p, and col.>
<Code draws the spirograph.>

Would you like to draw a spirograph? (Y/n): n

Exercise 5.2c

Add the ability for the user to draw multiple spirographs on top of each other by implementing the following modifications to the algorithm.

  1. Import the turtle and math libraries.
  2. Create the turtle window and a turtle (refer to a previous lab if you don't remember how to do this).
  3. Loop forever
    1. Prompt for and read choice indicating whether the user would like to draw another spirograph.
    2. If choice is ‘n’ or ‘N’:
      1. Break out of the loop.
    3. Else if choice is not ‘y’ or ‘Y’:
      1. print error message telling user to enter Y or n.
    4. Otherwise:
      1. Prompt for the moving radius and store this float in mov_rad.
      2. Prompt for the fixed radius and store this float in fix_rad.
      3. Prompt for the offset of the pen point and store this float in pen_offset.
      4. Prompt for the color and store this string in color.
      5. Draw the spirograph.
  4. Make it so the spirograph window closes when clicked.

Your code should handle these cases:

Checking In

We will grade these exercises according to the following criteria:

Submit your solutions to these lab exercises using your appropriate submission environment.

If you worked with a partner, make sure you both have a copy of the files.

If you’re working on a lab computer, don’t forget to log off of your machine when you are finished!