Computer Science 2611
Summer 1999
Laboratory Assignment 1

Terminology

For this assignment all numbers are positive integers.

Number k is a proper divisor of number n if k < n and k is a divisor of n. For example, 3 is a proper divisor of 6 and 5 is a proper divisor of 15.

A number n is perfect if the sum of the proper divisors of n is n. For example, the proper divisors of 6 are 1, 2 and 3. Since 1+2+3 = 6, 6 is a perfect number. Similarly, the proper divisors of 28 are 1, 2, 4, 7, and 14. Since 1+2+4+7+14 = 28, 28 is a perfect number.

Assignment, part 1

Write a C++ program that reads a number n from the user and tells whether n is perfect. It will need to have a loop that accumulates the sum of the proper divisors of n. Look at each number from 1 to n-1. For each one, if it is a divisor of n, add it into the total. In a C++ program you can test whether k is a divisor of n by testing whether n%k == 0.

Test your program. When it works, print it.

Assignment, part 2

Now modify your program from part 1. Make a copy of the file, so that you have the old one as a backup.

The modified program should read a number k from the user and print the first k perfect numbers. You will need to write a loop around the loop from the first assignment. Keep two different counters, one telling the number that you are currently testing for perfectness, the other telling how many perfect numbers have been printed. Put a comment in your program saying what each of those counters is for. Each time around the loop you will need to update the number that your are testing for perfectness. Only update the count of how many were printed when you print a number, and keep looping as long as you have printed fewer than k numbers.

Test your program. Only try printing the first 3 perfect numbers. (What is the third perfect number?) Print your program. Turn in both your first version and your second version together.