#format python """AmicablePair finding script, it also find PerfectNumber -- ["yong27"] [[DateTime(2006-08-31T06:50:45Z)]] """ import unittest factors = lambda n: [x for x in range(1, n) if not n % x] sumOfFactors = lambda n: sum(factors(n)) def printAmicablePairs(N): for x in range(1, N+1): xsf = sumOfFactors(x) if sumOfFactors(xsf) == x: print x, xsf class AmicablePairTest(unittest.TestCase): def testFactors(self): self.assertEquals([1,2,3], factors(6)) self.assertEquals([1,2,3,4,6], factors(12)) self.assertEquals([1,2,3,4,6], factors(12)) self.assertEquals([1,2,4,5,10,11,20,22,44,55,110], factors(220)) self.assertEquals([1,2,4,71,142], factors(284)) def testSumOfFactors(self): self.assertEquals(284, sumOfFactors(220)) self.assertEquals(220, sumOfFactors(284)) if __name__=='__main__': #unittest.main() printAmicablePairs(10000)