1 """AmicablePair finding script, it also find PerfectNumber
2 -- ["yong27"] [[DateTime(2006-08-31T06:50:45Z)]]
3 """
4
5 import unittest
6
7 factors = lambda n: [x for x in range(1, n) if not n % x]
8 sumOfFactors = lambda n: sum(factors(n))
9
10 def printAmicablePairs(N):
11 for x in range(1, N+1):
12 xsf = sumOfFactors(x)
13 if sumOfFactors(xsf) == x:
14 print x, xsf
15
16 class AmicablePairTest(unittest.TestCase):
17 def testFactors(self):
18 self.assertEquals([1,2,3], factors(6))
19 self.assertEquals([1,2,3,4,6], factors(12))
20 self.assertEquals([1,2,3,4,6], factors(12))
21 self.assertEquals([1,2,4,5,10,11,20,22,44,55,110], factors(220))
22 self.assertEquals([1,2,4,71,142], factors(284))
23
24 def testSumOfFactors(self):
25 self.assertEquals(284, sumOfFactors(220))
26 self.assertEquals(220, sumOfFactors(284))
27
28 if __name__=='__main__':
29
30 printAmicablePairs(10000)