Пример #1
0
24. By mathematical analysis, it can be shown that all integers greater
than 28123 can be written as the sum of two abundant numbers. However,
this upper limit cannot be reduced any further by analysis even though it
is known that the greatest number that cannot be expressed as the sum of
two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the
sum of two abundant numbers.
"""

from utils import proper_factors

UPPER = 28123

abundant = []
for i in range(2, UPPER+1):
  if sum(proper_factors(i))>i:
    abundant.append(i)


# get all number that CAN be written as the sum of two abundant numbers
pair_abundant = set()
for i, a in enumerate(abundant[:UPPER]):
  for b in abundant[:i+1]:
    if a+b<=UPPER:
      pair_abundant.add(a+b)

# the sum of all numbers that CANNOT be written as the sum of two abundant numbers
# is just th sum of all numbers [1, UPPER] minus the sum of the abundant pairs
print UPPER*(UPPER+1)/2 - sum(pair_abundant)
Пример #2
0
For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22,
44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1,
2, 4, 71 and 142; so d(284) = 220.

Evaluate the sum of all the amicable numbers under 10000.
"""

from utils import proper_factors
from collections import defaultdict

UPPER = 10000

d = {}
inv_d = defaultdict(set)
for i in range(1, UPPER):
  d[i] = sum(proper_factors(i))
  inv_d[d[i]].add(i)
  # need to keep track of amicable siblings that may lie above the upper bound
  if d[i] > UPPER:
    d[d[i]] = sum(proper_factors(d[i]))
    inv_d[d[d[i]]].add(d[d[i]])

amicable = set()
for a in range(1, UPPER):
  for b in inv_d[a]:
    if a!=b and d[a]==b and d[b]==a:
      amicable.add(a)
      if b<UPPER:
        amicable.add(b)

print sum(amicable)