2014-05-31 14:33:30 -07:00
|
|
|
# Copyright (C) 2014 kcotugno
|
2014-05-30 18:41:46 -07:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
|
|
|
# LICENSE file or http://www.opensource.org/licenses/MIT
|
|
|
|
#
|
2014-05-31 14:33:30 -07:00
|
|
|
# Author: kcotugno
|
2014-05-30 18:41:46 -07:00
|
|
|
# Date 5/30/14
|
|
|
|
|
|
|
|
import math
|
|
|
|
|
|
|
|
|
2014-05-31 14:33:30 -07:00
|
|
|
# This function check if a number is prime first by testing for the #2, then if
|
|
|
|
# it less than 2 or divisible by 2. If non of these pre-checks return, it runs
|
|
|
|
# through a loop which goes from 3 to the sqrt of the number only testing it
|
|
|
|
# against odd numbers.
|
2014-05-30 18:41:46 -07:00
|
|
|
def isprime(num):
|
2014-05-31 14:33:30 -07:00
|
|
|
if num == 2:
|
|
|
|
return True
|
2014-05-30 18:41:46 -07:00
|
|
|
|
2014-05-31 14:33:30 -07:00
|
|
|
if num < 2 or num % 2 == 0:
|
|
|
|
return False
|
2014-05-30 18:41:46 -07:00
|
|
|
|
2014-05-31 14:33:30 -07:00
|
|
|
for i in range(3, int(math.sqrt(num)) + 1, 2):
|
|
|
|
if num % i == 0:
|
2014-05-30 18:41:46 -07:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# The algorithm calculates the numbers by continually finding numbers in double
|
|
|
|
# proportion from 1 until the sum of them is a prime number. Then, it takes the
|
|
|
|
# product of the prime and last number in the proportion. Whence perfect numbers
|
|
|
|
# are found.
|
|
|
|
def perfection(calc):
|
|
|
|
proportionals = [1]
|
|
|
|
perfect = []
|
|
|
|
|
|
|
|
p = 0
|
|
|
|
while p < calc:
|
|
|
|
|
|
|
|
psum = 0
|
|
|
|
while isprime(psum) == False:
|
|
|
|
proportionals.append((proportionals[len(proportionals) - 1]) \
|
|
|
|
* 2)
|
|
|
|
psum = 0
|
|
|
|
for i in proportionals:
|
|
|
|
psum = psum + i
|
|
|
|
#print(psum)
|
|
|
|
|
|
|
|
p += 1
|
|
|
|
perfect.append(psum * proportionals[len(proportionals) - 1])
|
|
|
|
|
|
|
|
return perfect
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2014-05-31 14:33:30 -07:00
|
|
|
print("Euclidean Perfection")
|
2014-05-30 18:41:46 -07:00
|
|
|
print("Copyright (C) 2014 kcotugno")
|
|
|
|
print("Distributed under the MIT software license, see the accompanying")
|
|
|
|
print("LICENSE file or http://www.opensource.org/licenses/MIT\n")
|
|
|
|
|
|
|
|
gotinput = False
|
|
|
|
|
|
|
|
while gotinput == False:
|
|
|
|
i = input("Please enter the desired number of perfection "
|
|
|
|
"to calculate: ")
|
|
|
|
|
|
|
|
try:
|
|
|
|
int(i)
|
|
|
|
|
|
|
|
except:
|
|
|
|
print("Please only enter positive integers..."
|
|
|
|
"and not too large :)\n")
|
|
|
|
|
|
|
|
else:
|
|
|
|
i = int(i)
|
|
|
|
|
|
|
|
if i <= 0:
|
|
|
|
print("Please only enter positive integers..."
|
|
|
|
"and not too large :)\n")
|
|
|
|
|
|
|
|
else:
|
|
|
|
gotinput = True
|
|
|
|
|
|
|
|
perfect = perfection(i)
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
for p in perfect:
|
|
|
|
index += 1
|
|
|
|
print(format("{0}. {1}".format(index, p)))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|
|
|
|
|
|
|
|
|