Skip to content

Instantly share code, notes, and snippets.

@dmrz
Created August 9, 2013 22:31
Show Gist options
  • Save dmrz/6197845 to your computer and use it in GitHub Desktop.
Save dmrz/6197845 to your computer and use it in GitHub Desktop.
import collections
CACHE = collections.defaultdict(set)
CACHE[1].add(1)
def divisors(n):
if not CACHE[n]:
d = n
while d >= 1:
n % d or CACHE[d] and CACHE[n].update(CACHE[d]) or CACHE[n].add(d)
d -= 1
result = CACHE[n].copy()
if len(result) > 1:
result.remove(n)
return result
def divisors_sum(n):
return sum(divisors(n))
def main():
numbers = []
n = 1
while n < int(1e4):
number = divisors_sum(n)
if number != n and n == divisors_sum(number):
numbers.append(n)
n += 1
return sum(numbers)
if __name__ == '__main__':
result = main()
print(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment