Advertisement
brilliant_moves

PerfectNumbers.py

Dec 13th, 2014
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.57 KB | None | 0 0
  1. '''
  2. "PerfectNumbers.py"
  3. by Chris Clarke
  4. 13.12.2014
  5. '''
  6. def print_perfect():
  7.     for n in range(1, 10000):
  8.         sum_factors = 0 # reset
  9.         output = "" # reset
  10.         max = int(n/2) # highest factor of n is half of n
  11.         for i in range(1, max+1):
  12.             if n%i == 0: # i is a factor of n
  13.                 if (i==1): # first iteration of inner loop
  14.                     output = str(n)+" = 1"
  15.                 else: # 2nd, etc
  16.                     output += " + "+str(i)
  17.                 sum_factors += i
  18.  
  19.         if sum_factors==n: # ...it's perfect
  20.             print output # print string
  21.  
  22. def main():
  23.     print "The perfect numbers are"
  24.     print_perfect()
  25.  
  26. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement