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
You might also like to view...
The code used for pull-down menus in a GUI system is:
A) classification codes. B) alphabetic deviation codes. C) block sequence codes. D) cipher codes. E) simple sequence codes.
A(n) ________ chart is a chart on a sheet with the data
Fill in the blank(s) with correct word
Fragmenting information into too many subcategories makes the Web site more difficult to ____.
A. read B. navigate C. access D. find on the Web
Internet banking customers currently use an account number and password to access their online accounts. The bank wants to improve security on high value transfers by implementing a system which call users back on a mobile phone to authenticate the transaction with voice verification. Which of the following authentication factors are being used by the bank?
A. Something you know, something you do, and something you have B. Something you do, somewhere you are, and something you have C. Something you are, something you do and something you know D. Something you have, something you are, and something you know