CIS 220 - Program 3
Objectives: Gain experience using control structures.Description: This program is intended to provide you with experience using sequence, selection, and iteration. You will use nested if statements as well as nested loops.
A perfect number is a number that equals the sum of its factors, excluding the number itself. For example, 6 is a perfect number because the factors of 6 are 1, 2, 3, and 6. Excluding 6, the factors 1 + 2 + 3 equal 6, making it a perfect number.
Write a program in which the user inputs the number of perfect numbers to calculate, and then will determine that quantity of perfect numbers, excluding 1.
Your program requires an outer loop that finds candidate perfect numbers by continuing to loop until the required number of perfect numbers have been found. The formula to determine candidate perfect numbers is
2n - 2n\2
with n being incremented by 2 with each iteration since only odd exponents (Ns) have been found to be associated with perfect numbers. For example, with n=1 you test 21-21\2, then with n=2 you test 22-22\2, with n=3 you test 23-23\2, etc., until the required number of perfect numbers have been found. (Note that 23-23\2 is 8 - 2, or 6, which is your first perfect number.) For each candidate perfect number you must determine if the sum of the factors is equal to the number.
Therefore you will need a nested loop to determine the factors of each number under consideration. A number is a factor if (perfectCandidate Mod number = 0). The program must sum the factors to see if their total equals the number itself. It should be adequate to loop from 1 to (perfectCandidate \ 2).
Here is an abbreviated algorithm:
determine perfect candidate using 2^n - 2^(n \ 2)
Loop until all factors found
if perfectCandidate Mod number = 0 then
add factor to sum of factors
Use a text box for user input, a command button to initiate program execution, and a list box to display your results. You also need to clear the list box when the command button is pressed in order to clear previous output before a new test. Beep when all numbers have been found.
Your GUI should resemble the following as closely as possible. Be sure that you exclude 1.
Finding more than four perfect numbers can take a long time because the fifth perfect number is greater than 33,550,300, but the sixth perfect number is not found until 8,589,869,056. (The seventh is 137,438,691,328.) The delay between finding the fourth and fifth perfect number, while relatively short, is still noticeable. A computer with a 2.80 GHz Pentium D Processor 820 with Dual Core Technology can find the first five perfect numbers in two seconds, while finding the first six requires just over 16 minutes. University computers will take noticeably longer. The number of digits in the larger perfect numbers also means that you will have to select one of the larger integer data types. Be sure at least one of your test runs attempts to find six perfect numbers, because this will expose any problems with data type selection.
Your program should print a warning message in a critical message box that it will not calculate more than six perfect numbers and then force the user to re-enter a value.
If the user requests more than five perfect numbers your program should warn the user with a message box (exclamation icon) that the results may be delayed. (See the demo for sample messages.)
Even with the smaller perfect numbers, unless your program indicates that it is still performing calculations, the user may think that the program has locked up. In order to provide some sign that the program is still executing, use the command Me.Cursor = Cursors.WaitCursor to change the shape of the cursor to an hourglass. When all perfect numbers have been found, reset the mouse pointer to the default with the command Me.Cursor = Cursors.Default. This is an example of how the user interface can be designed to provide information about program status to the user.
You can find more details about this algorithm here. Be sure to provide details of your algorithm in either pseudocode or flowchart and turn it in with your program.
|6||23 - 21|
|28||25 - 22|
|496||29 - 24|
|8,128||213 - 26|
|33,550,336||225 - 212|
|8,589,869,056||233 - 216|