-
Notifications
You must be signed in to change notification settings - Fork 2
/
p021.py
55 lines (48 loc) · 1.07 KB
/
p021.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python
#
# Project Euler
#
# Problem 21
#
# 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.
#
# Evaluate the sum of all the amicable numbers under 10000.
def d(n): # sum of all proper factors
if n==1: return 0 # 1 has no proper factors
# first, find factors
factors=[]
for i in xrange(1,int(n**(0.5)+1)):
if n%i==0:
ndivi=n/i
if ndivi==i or i==1: factors.append(i)
else:
#print i,ndivi
factors.append(i)
factors.append(ndivi)
# second, find sum and return
return sum(factors)
if __name__ == "__main__":
amicables=[]
numstested=[]
for a in xrange(1,10000):
b=d(a)
if b in numstested: continue
db=d(b)
if a!=b and a==db:
amicables.append(a)
amicables.append(b)
numstested.append(a)
numstested.append(b)
print sum(amicables)
'''a=220
b=d(a)
print b
#if b in numstested: continue
#compound=d(d(a))
#db=d(b)
if a!=b and a==b:
print "Amicable!"
amicables.append(a)
amicables.append(b)
numstested.append(a)
numstested.append(b)'''