The purpose of today's lab is to practice using repetition.
There are 2 components:
- Finding perfect numbers
- Drawing Spirographs
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:
- Prompt for a positive integer and store
it as value
- Initialize low to be 1
- Initialize high to be the same as
value
- Initialize divisors to be an empty
list
- Repeat as long as low is less than
high:
- If low is a divisor of value:
- Set high to be value
integer divided by low
- Add low to the list of divisors
- Add high to the list of divisors ONLY if it is
different from low
- 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:
- You will need to remove value from the list of
divisors. You can do this using
divisors.remove(value)
- To compute the sum of a list you can use the
sum()
function.
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:
- At the top of your code, add a variable to keep track of how
many perfect numbers have been found. Initialize this variable to 0.
- Replace your code asking for user input
with the beginning of a
for
loop that uses value
as the loop variable and ranges from 2 to 10000 (1 is just weird...
we don't want it). Indent all of the rest of your code to be within
this for loop.
- Add a check at the bottom of your code (within the for loop)
that checks if we have found four perfect numbers. When we have,
break out of the loop.
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:
- Import the turtle and math libraries.
- Create the turtle window and a turtle (refer to a previous
lab if you don't remember how to do this).
- Prompt for the moving radius and store this
float in mov_rad.
- Prompt for the fixed radius and store this
float in fix_rad.
- Prompt for the offset of the pen point and
store this float in pen_offset.
- Prompt for the color and store
this string in color.
- Draw the spirograph.
- 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.
- # First, do the setup.
- Initialize the current time to 0.0.
- Tell your turtle to pick up its pen.
- Tell your turtle to move to the appropriate initial
position. In particular, use the equations given above to compute
x(0) and y(0).
- Tell your turtle to put the pen down.
- Tell your turtle to set the pen color to the value given by
the user.
- # Then draw the spirograph.
- Loop while the time is less than 100.
- Increment time by 0.1.
- Compute a value for x using the new value of time. Use the
equations specified above.
- Compute a value for y using the new value of time.
- 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:
- 55, 53, 77, and gold
- 75, 23, 17, and maroon
- 79, 74, 38, and blue
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.
- Import the turtle and math
libraries.
- Create the turtle window and a turtle (refer
to a previous lab if you don't remember how to do this).
- Loop forever
- Prompt for and read choice
indicating whether the user would like to draw another spirograph.
- If choice is ‘n’ or
‘N’:
- Break out of the loop.
- Else if choice is not
‘y’ or ‘Y’:
- print error message telling user to enter Y or n.
- Otherwise:
- Prompt for the moving
radius and store this float in mov_rad.
- Prompt for the fixed
radius and store this float in fix_rad.
- Prompt for the offset of
the pen point and store this float in pen_offset.
- Prompt for the color and
store this string in color.
- Draw the spirograph.
- Make it so the spirograph window closes when
clicked.
Your code should handle these cases:
- Y or y: ask for the 4 values and make the spirograph.
- N or n: break out of the while loop, so the code goes to the
very end (the exitonclick call).
- User hits enter only: treat it just the same as Y or
y.
- Anything else: Print a message saying "please enter Y or
n", and then ask again for input.
Checking In
We will grade these exercises according to the following
criteria:
- Correctness:
- 45% - Exercise 1: Perfect numbers correctly identifies the
4 perfect numbers less than 10000.
- 40% - Exercise 2: Spirograph drawing works correctly.
- Understandability:
- 5% - Header Documentation - Document the code’s basic
purpose, authors and assignment number.
- 5% - Code Documentation - Separate the logical blocks of
your programs with useful comments and white space.
- Usability:
- 5% - User Interaction - The spirograph interaction is intuitive for users.
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!