dSum=[1 for i in range(10000)] for d in range(2,10000): for i in range(d+d,10000,d): dSum[i]+=d print sum(i for i in range(2,10000) if dSum[i]<10000 and dSum[i]!=i and dSum[dSum[i]]==i)