MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / solution

Function solution

project_euler/problem_023/sol1.py:22–48  ·  view source on GitHub ↗

Finds the sum of all the positive integers which cannot be written as the sum of two abundant numbers as described by the statement above. >>> solution() 4179871

(limit=28123)

Source from the content-addressed store, hash-verified

20
21
22def solution(limit=28123):
23 """
24 Finds the sum of all the positive integers which cannot be written as
25 the sum of two abundant numbers
26 as described by the statement above.
27
28 >>> solution()
29 4179871
30 """
31 sum_divs = [1] * (limit + 1)
32
33 for i in range(2, int(limit**0.5) + 1):
34 sum_divs[i * i] += i
35 for k in range(i + 1, limit // i + 1):
36 sum_divs[k * i] += k + i
37
38 abundants = set()
39 res = 0
40
41 for n in range(1, limit + 1):
42 if sum_divs[n] > n:
43 abundants.add(n)
44
45 if not any((n - a in abundants) for a in abundants):
46 res += n
47
48 return res
49
50
51if __name__ == "__main__":

Callers 1

sol1.pyFile · 0.70

Calls 1

addMethod · 0.45

Tested by

no test coverage detected