Finished 23, pretty darn slow and ugly solution

This commit is contained in:
2020-04-14 00:37:45 +02:00
parent e52795b032
commit 02dea31c31
2 changed files with 63 additions and 0 deletions

View File

@@ -2,6 +2,9 @@ import os
import math import math
import numpy as np import numpy as np
# Calculate the sum of all scores of the names in the textfile, where
# the score is the product of the sorted position and
# sum of alphabetic values of its letters
def charScore(c): def charScore(c):
assert len(c) == 1 assert len(c) == 1

60
23/main.py Normal file
View File

@@ -0,0 +1,60 @@
import os
import math
import numpy as np
# Find the sum of all positive integers which cannot be written as the sum
# of two abundant numbers.
# A number is abundant if its properdivisors sum to
# more than the original number, e.g. 12 is abundant, since 12 can be divided
# by 1, 2, 3, 4, 6 = 16
# It can be proven that all numbers above 28123 can be written as the sum
# of 2 abundant numbers
def getDivisors(n):
divs = [True] * math.ceil(n/2)
for x in range(1, len(divs)):
if divs[x] and n % (x + 1) != 0:
y = x
while y < len(divs):
divs[y] = False
y = y + x + 1
res = []
for x in range(len(divs)):
if divs[x]:
res.append(x + 1)
return res
def isAbundant(n):
return sum(getDivisors(n)) > n
def main():
print("Hello, this is Patrick")
m = 28123
a = list(filter(isAbundant, range(1, m+1)))
# We are assuming that the abundants are sorted
sums = set([])
for i in range(len(a)):
for j in range(i, len(a)):
s = a[i] + a[j]
if s > m:
break
else:
#print(s)
sums.add(s)
nonsums = set(range(1, m+1))
nonsums.difference_update(sums)
# print(nonsums)
print(sum(nonsums))
if __name__ == "__main__":
main()