Advertisement
makispaiktis

Problem 3 - Perfect Numbers forever

Jul 5th, 2021 (edited)
1,228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.70 KB | None | 0 0
  1. # Function 1 - Decides if a number is perfect
  2. def isPerfect(n):
  3.     # I have to find the divisors until n/2 = the biggest divisor of n
  4.     divisors = [1]
  5.     for i in range(2, int(n/2)+1):
  6.         if n % i == 0:
  7.             divisors.append(i)
  8.     SUM = sum(divisors)
  9.     if SUM == n:
  10.         return True, divisors
  11.     return False, []
  12.  
  13. # MAIN FUNCTION
  14. limit = 10**4
  15. print("Scanning from 4 to " + str(limit))
  16. print()
  17. for n in range(4, limit+1):
  18.     flag, Divisors = isPerfect(n)
  19.     if flag:
  20.         divisors = [str(Divisors[i]) for i in range(len(Divisors))]
  21.         divisorsString = " + ".join(divisors)
  22.         print(str(n) + " is a perfect number:    " + str(n) + " = " + divisorsString)
  23.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement