# Programmer: Sriram Pemmaraju # Date: 2/19/2012 # This program reads a positive integer M and prints out all # perfect integers <= M. Recall that a perfect integer x is one # whose factors (excluding x) add to x. # Function that returns the sum of the factors of a given positive # integer x, excluding x. def factorSum(x): factor = 1 sum = 0 # Run through candidate factors to identify real factors of x while factor <= x/2: if x % factor == 0: # factor is a real factor of x sum = sum + factor factor = factor + 1 return sum # Boolean function that determines if the given integer y # is perfect or not. def isPerfect(y): return y == factorSum(y) # Main program: mainly for input and output and for # running through integers 2 <= n <= M, and outputting # those that are perfect. M = int(raw_input("Please type a positive integer. ")) n = 2 while n <= M: if isPerfect(n): print n n = n + 1