Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Find perfect Numbers based on the Euclid-Euler Theorem
- # Mike Kerry - Sept 2022
- #
- # Consider the sequence 1, 2, 4, 8, 16 ....
- # If the sum of this sequence is prime, then
- # the product of the sum and the last in the sequence is perfect
- def is_prime(num):
- if num < 4:
- return num > 1 # 2 and 3 => True, anything less than 2 is False
- if not num % 2:
- return False # All even numbers above 2, are non-prime
- for d in range(3, 1 + int(num ** 0.5), 2):
- if not num % d:
- return False
- return True
- ctr, tot = 0, 1
- n = 1
- while ctr < 8:
- n += n # double-up n each iteration
- tot += n # sum of the sequence 1, 2, 4, 8, ...
- if is_prime(tot): # if this sum is prime ...
- print(n * tot) # ... then (sum * last) is 'perfect'
- ctr += 1
- # Output
- # 6
- # 28
- # 496
- # 8128
- # 33550336
- # 8589869056
- # 137438691328
- # 2305843008139952128
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement