COS 100: Introduction to Programming
Fall 2019
Lab 5: Repeating again
In-class practice labs will not directly impact your grade
(although you'll get your standard participation point for staying on task),
and will serve to help you with homework projects.
Lab 5A: Am I a perfect square?
-
Math fact: the sum of the first $n$ odd numbers is equal to $n^2$.
-
For example: $1+3+5+7=16=4^2$.
-
In this lab, you'll try to verify this fact by numerical experiment.
-
Task:
Prompt the user for a positive integer $n$.
-
Calculate the sum of the first $n$ odd numbers and print out the sum.
-
Also print out $n^2$.
-
Sample run:
Number, please? 4
The sum of the first 4 odd numbers is 16
Incidentally, the square of 4 is also 16
-
Hint:
During development, you may want to print out the numbers you are adding, e.g.:
Number, please? 4
1
3
5
7
The sum of the first 4 odd numbers is 16
Incidentally, the square of 4 is also 16
Lab 5B: Am I a prime?
-
Task:
Prompt the user for an integer and print out whether the number is prime or not.
-
If the user enters a number less than or equal to 1, it is not prime; say so and exit.
-
An integer is always evenly divisible by 1 and itself.
An integer greater than 1 is prime if it is not evenly divisible by any positive integer besides 1 and itself.
-
So, given a number, try to divide it by all the numbers between 1 and itself to see if any numbers are factors.
If you can't find any factors other than 1 and itself, it's prime.
-
For example, 10 is evenly divisible by 2, so 2 is a factor of 10, and 2 testifies that 10 is not a prime.
-
The first ten prime numbers are 2, 3, 5, 7, 11, 13, 17, 19, 23, and 29.
- After thinking/trying for a while, if you are stuck, click to reveal an important hint
-
The first time they do a program like this, most people will do something like this (shown in pseudocode):
for i in range(2, num):
if i divides number:
print("not a prime")
else:
print("prime")
-
At first glance, this seems like a good plan, but there's a problem:
If our number is, say, 9, then we will start
i
at 2.
Since 2 doesn't divide 9, we print "prime".
However, 9 isn't a prime number (it's divisible by 3).
-
You can't declare a number to be prime unless you've checked all of the
possible divisors, and you've determined that none of them divides the number
in question.
- Click to reveal a further hint
Hint:
What if you had a boolean variable that you created at the very beginning of
the program? This variable's job could be to keep track of whether the number
still has a chance of being prime. If you ever did find a divisor, you could
set the variable to False
because it can't possibly be prime any
more. At the very end of the program, the value of that variable would tell
you whether a divisor was ever found or not.
-
There are actually a few earlier stopping points without going all the way up to the number you're testing. If you think you have one, ask the TA or professor to verify that your program is still correct.
Challenge Lab 5C: Verify a math fact more thoroughly