Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function 1 - Decides if a number is perfect
- def isPerfect(n):
- # I have to find the divisors until n/2 = the biggest divisor of n
- divisors = [1]
- for i in range(2, int(n/2)+1):
- if n % i == 0:
- divisors.append(i)
- SUM = sum(divisors)
- if SUM == n:
- return True, divisors
- return False, []
- # MAIN FUNCTION
- limit = 10**4
- print("Scanning from 4 to " + str(limit))
- print()
- for n in range(4, limit+1):
- flag, Divisors = isPerfect(n)
- if flag:
- divisors = [str(Divisors[i]) for i in range(len(Divisors))]
- divisorsString = " + ".join(divisors)
- print(str(n) + " is a perfect number: " + str(n) + " = " + divisorsString)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement