A proper factor of a positive integer n is a (positive) factor of n that is less than n. For example, 4 is a proper factor of 12. There are exactly three proper factors of 6. They are 1, 2 and 3.

A positive integer is defined to be perfect if it is equal to the sum of its proper factors. For example, 6 is perfect since the proper factors of 6 are 1, 2 and 3, and 1 + 2 + 3 = 6. 8 is not perfect since the proper factors of 8 are 1, 2 and 4, and 1 + 2 + 4 ≠ 8. Mathematicians look at perfect numbers because there is a connection between them and certain prime numbers.

  1. Write a definition of a function isPerfect(n) that yields true if n is perfect and false if not. Use a loop to accumulate the sum of the proper factors of n, then check whether that sum is equal to n.

  2. Add an Execute part that displays the first three perfect numbers. Use a loop. Be sure to count how many numbers have been displayed. You are supposed to display the first three perfect numbers, not the perfect numbers that are less than or equal to three.

    Use your isPerfect function to test whether a number is perfect. So you will find yourself writing something like

      If isPerfect(n) then
    
    inside a loop, where you display n and add 1 to your count if n is perfect.

 

  [Language: Cinnameg  Kind: program]