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)
| 20 | |
| 21 | |
| 22 | def 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 | |
| 51 | if __name__ == "__main__": |