From dfa586dc1f2514ea81cb1d2d13fd9c40279dc71b Mon Sep 17 00:00:00 2001 From: Correl Roush Date: Wed, 14 Apr 2010 20:58:26 +0000 Subject: [PATCH] Problem 021 git-svn-id: file:///srv/svn/euler@48 e5f4c3ec-3c0c-11df-b522-21efaa4426b5 --- e021.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 e021.py diff --git a/e021.py b/e021.py new file mode 100644 index 0000000..791fe5e --- /dev/null +++ b/e021.py @@ -0,0 +1,28 @@ +from e007 import is_prime +from e012 import factor + +MIN = 2 +MAX = 10000 + +def proper_divisors(n): + """Returns the proper divisors of n + + Proper divisors are defined as numbers less than n which divide evenly into n + """ + divisors = factor(n) + # Knock off the last factor, since it is equal to n + return divisors[:-1] + +sums = {} +amicable = [] +i = MIN +while i < MAX: + if not is_prime(i): + s = sum(proper_divisors(i)) + sums[i] = s + if s in sums and i == sums[s] and i != s: + print i, s, sums[s] + amicable.append(i) + amicable.append(s) + i = i + 1 +print 'Sum of amicable numbers less than {0}: {1}'.format(MAX, sum(amicable)) \ No newline at end of file