Computer Science 2611
Spring 2003
Laboratory Assignment 2

Date assigned: 1/28/03

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. The number 1 is a proper divisor of all numbers that are greater than 1.

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 function called isperfect that takes a positive integer n and returns 1 if n is perfect, 0 if n is not perfect. The heading should be as follows.

    bool isperfect(int n)
Write a contract for your function, and then write the function body. Be sure that the contract is clear, concise and precise. If the contract does not mention the role of n, it cannot possibly be precise. If it does not state what is returned, it is not precise.

Write a main program that tries function isperfect on a few fixed values, including some where the answer is 1, and some where the answer is 0. Print each answer, along with an indication of the number that is being tested. Do not just print 0's and 1's. Run your program. Check the answers. Does it appear to work?

Assignment, part 2

For this part, replace the main program from part 1 with a program that reads a number N from the user and prints the first N perfect numbers. For example, if N = 3, then it prints the first 3 perfect numbers.

Test your program. Note. You can easily get the first four perfect numbers, but the fifth one is quite large. You program will not be able to find it.

What to turn in

Turn in a printout of each part. There should be two variants turned in.