Complete task

This commit is contained in:
2020-10-12 20:02:04 +02:00
parent 7201fa6e8b
commit 20933b4f16

View File

@@ -1,10 +1,7 @@
import math
from functools import reduce
def factors(x):
result = [1]
i = 2
while i*i <= x: #Other way of writing i<=sqrt(x)
while i*i <= x:
if x % i == 0:
result.append(i)
if x//i != i:
@@ -12,39 +9,18 @@ def factors(x):
i += 1
return result
def is_abundant(n):
return sum(factors(n)) > n
def isAbundant(x):
return sum(factors(x)) > x
abundants= [i for i in range(28123) if is_abundant(i)]
abundantNumbers = [num for num in range(1, 28123) if isAbundant(num)]
sums = [True for _ in range(1,28123)]
range28 = [range(28123+1)]
abundantsums = []
result = []
for i in range(len(abundants)):
for j in range(len(abundants)-i):
if abundants[i]+abundants[i-j]<=28123 and abundants[i]+abundants[i-j] in range28:
abundantsums.append(abundants[i]+abundants[i-j])
range28.pop(range28.index(abundants[i]+abundants[i-j]))
result.append(abundants[i]+abundants[i-j])
print(len(abundantsums)) #24 266 061
print("calculated abundantsums")
for i in range(len(abundantNumbers)):
for k in range(len(abundantNumbers)):
try:
sums[abundantNumbers[i] + abundantNumbers[k]] = False
except IndexError:
pass
#result = sum([i for i in range(28123+1) if not i in abundantsums])
# result = 0
# for i in range(28123+1):
# print(i)
# if not i in abundantsums:
# result+=i
# range28 = [range(28123+1)]
# result = []
# for i in abundantsums:
# if i in range28:
# range28.pop(range28.index(i))
# result.append(i)
print(result)
print(sum([i for i, x in enumerate(sums) if x]))