-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheuler-095.py
97 lines (90 loc) · 1.79 KB
/
euler-095.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env python
import psyco
psyco.full()
#def primes(x) :
# a = range(0,x+1)
# a[0], a[1] = 0, 0
# for i in xrange(2, x+1) :
# if a[i] == i :
# a[i] = 1
# if (x+1)/ i > i :
# for j in xrange(i*i, x+1, i) :
# a[j] = i
# #return [x for x, aa in enumerate(a) if aa == 1]
# return a
#
#def prime_factors(a) :
# global p
# if a > len(p) :
# return [ a ]
# else :
# result = []
# while p[a] != 1 :
# result.append(p[a])
# a /= p[a]
# result.append(a)
# return result
#
#def product(s) :
# result = 1
# for ss in s :
# result *= ss
# return result
#
#def choose(s, i) :
# if i == 0 :
# yield []
# else :
# for x in xrange(len(s)) :
# c = s[x]
# temp = s[x+1 : ]
# for cc in choose(temp, i - 1) :
# yield [c] + cc
# return
#
#def proper_divisors(i, pf) :
# results = [1]
# for j in xrange(1, len(pf)) :
# for x in choose(pf, j) :
# px = product(x)
# results.append(px)
# return set(results)
def create_proper_divisors(limit):
table = [1] * limit
for n in xrange(2, limit):
for m in xrange(n + n, limit, n):
table[m] += n
return table
def test() :
m = 1000 * 1000
print "Generating prime factors and proper divisors"
pd = create_proper_divisors(m)
print "Creating chains"
chains = []
for i, p in enumerate(pd) :
#if i % 1000 == 0 :
# print ".",
if p != 1 :
chain = []
while True :
chain.append(i)
i = pd[i]
if i > m : break
elif i in chain :
j = chain.index(i)
cc = chain[j : ]
if len(cc) > 1 :
chains.append(cc)
for j in cc :
pd[j] = 1
break
print "writing out chains"
sc = [ (len(chain), min(chain), chain) for chain in chains ]
for scc in sorted(sc) :
print scc
if __name__ == "__main__" :
import time
s = time.clock()
test()
e = time.clock()
print "Elapsed time: ", (e - s)