/
021.py
73 lines (63 loc) · 1.79 KB
/
021.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# Let d(n) be defined as the sum of proper divisors of n (numbers less than n which divide evenly into n).
# 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.
# For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefore d(220) = 284. The proper divisors of 284 are 1, 2, 4, 71 and 142; so d(284) = 220.
# Evaluate the sum of all the amicable numbers under 10000.
import prime
import time
def prob21(limit):
cache = []
miss = [0, 1, 2]
sieve = prime.prime_table(limit+1)
# 1. Prime numbers are not amicable numbers
for i in range(1, len(sieve)):
if (sieve[i] == True):
miss.append(2*i+1)
for i in range(3, limit+1):
# 2. Find i in cache and miss
if not (i in cache or i in miss):
# 3. Compute sum of divisors for i
p = sum_of_divisors(i)
if (i != p and i == sum_of_divisors(p)):
cache.append(i)
if (p not in cache):
cache.append(p)
print("result: ", i, p)
else:
if (p != i):
miss.append(i)
miss.append(p)
else:
miss.append(i)
return sum(i for i in cache)
# Find the sum of proper divisors of a positive integer
def sum_of_divisors(n):
sum_divisors = 1
i = 2
while (i*i <= n):
if (n % i == 0):
sum_divisors += i
if (i*i != n):
sum_divisors += int(n/i)
i += 1
return sum_divisors
def sum_of_divisors_2(n, sieve):
sum_divisors = 1
k = n
for i in range(0, len(sieve)):
if (i == 0):
prime = 2
else:
prime = 2*i+1
exponent = 0
while (k % prime == 0):
k /= prime
exponent += 1
if (exponent > 0):
sum_divisors *= int((prime**(exponent+1) - 1)/(prime-1))
if (k == 1):
break
return sum_divisors - n
s = time.time()
print(prob21(10**4))
exec_time = time.time() - s
print("Exec time: %.3fs" % exec_time)