Skip to content

Instantly share code, notes, and snippets.

@kaipakartik
Created January 11, 2014 17:39
Show Gist options
  • Save kaipakartik/8374092 to your computer and use it in GitHub Desktop.
Save kaipakartik/8374092 to your computer and use it in GitHub Desktop.
Amicable numbers Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n). If d(a) = b and d(b) = a, where a ≠ b, then a and b are an amicable pair and each of a and b are called amicable numbers. For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220…
amicable = {}
d = [0 for x in xrange(10000)]
def getSumOfDivisors(n):
divisors = [1]
i = 2
while n / i > i:
if n % i == 0:
divisors.append(i)
divisors.append(n/i)
i += 1
return sum(divisors)
for i in xrange(2, 10000):
d[i] = getSumOfDivisors(i)
for a in xrange(2, 10000):
b = d[a]
try:
if a == d[b] and a != d[a]:
amicable[a] = True
# print a, d[a], d[b]
except IndexError:
pass
values = [key for key in amicable]
# print values
print sum(values)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment