An integer number is said to be a perfect number if the sum of its factors, including 1 (but not the number itself), is equal to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a function perfect that determines whether parameter number is a perfect number. Use this function in a program that determines and prints all the perfect numbers between 1 and 1000. Print the
factors of each perfect number to confirm that the number is indeed perfect. Challenge the power of your computer by testing numbers much larger than 1000.
What will be an ideal response?
```
# Displays the perfect numbers from 1 to 1000 and their factors.
# returns 1 if a number is perfect, 0 if not
def perfect( number ):
factorSum = 0 # holds the sum of factors
# test each possible factor from 1 to number - 1, inclusive
for possibleFactor in range( 1, number ):
# sum factors
if number % possibleFactor == 0:
factorSum += possibleFactor
if factorSum == number:
return 1 # perfect number
else:
return 0
# prints a number with its factors in the form:
# number = factor1 + factor2 + . . .
def displayFactors( number ):
print number, "=",
for possibleFactor in range( 1, number ):
if number % possibleFactor == 0:
print possibleFactor,
print # new line
# displays the perfect numbers from 1 to 1000
for integer in range( 1, 1000 ):
if perfect( integer ):
displayFactors( integer )
```
6 = 1 2 3
28 = 1 2 4 7 14
496 = 1 2 4 8 16 31 62 124 248