-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem_023_04.py
More file actions
61 lines (50 loc) · 1.84 KB
/
problem_023_04.py
File metadata and controls
61 lines (50 loc) · 1.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import time
import functools
def main():
# Find the sum of all positive integers that cannot be written as the sum of two abundant numbers
# NOTE: Only evaluate integers under 20162
# NOTE: every EVEN integer greater than 46 is the sum of two abundant numbers
# finds abundantNumbers under 20162
print("Finding Abundant Numbers")
abundantNumberList = findAbundantNumbers()
abundantNumberList.sort()
# creates an odd abundant numbers list and some small evens to lower number of additions
print("Truncating List")
oddList = set()
for number in abundantNumberList:
if number%2!=0 or number < 46:
oddList.add(number)
oddList = list(oddList)
oddList.sort()
# finds the possible sums of abundant numbers
print("Finding Possible Sums")
possibleSums = set()
for number2 in oddList:
for number in abundantNumberList:
tempSum = number + number2
if tempSum > 20162: break
possibleSums.add(tempSum)
# finds all positive integers that cannot be written as the sum of two abundant numbers
notSums = set()
for i in range(1, 20162):
if not i in possibleSums:
notSums.add(i)
notSums = list(notSums)
notSums.sort()
# prints sum of all positive integers that cannot be written as the sum of two abundant numbers
print("Answer:", sum(notSums))
def findAbundantNumbers():
abundantNumbers = set()
for i in range(12, 20162):
if sum(findProperDivisors(i)) > i: abundantNumbers.add(i)
return list(abundantNumbers)
def findProperDivisors(n):
divisorList = list(set(functools.reduce(list.__add__,
([i, n/i] for i in range(1, int(n**0.5) + 1) if n % i == 0))))
#sorts and takes off the last divisor (itself)
divisorList.sort()
return divisorList[:-1]
if __name__ == '__main__':
a = time.time()
main()
print(time.time() - a)