-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheuler-060.py
62 lines (56 loc) · 1.31 KB
/
euler-060.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
import psyco
psyco.full()
import sys
def testSolution(d, values) :
print >> sys.stderr, "Testing ", values, "\r",
for i,k in enumerate(values) :
v = values[ : i] + values[i+1 : ]
if any(x not in d[k] for x in v) :
break
else :
print
print values
def testAllKeys(d) :
for i in sorted(d.keys()) :
for j in d[i] :
if j > i :
for m in d[j] :
if m > j and m in d[i] :
for n in d[m] :
if n > m and n in d[j] :
for p in d[n] :
if p > n :
testSolution(d, [i,j,m,n,p])
#def testFourKeys(d) :
# for i in sorted(d.keys()) :
# for j in d[i] :
# if j > i :
# for m in d[j] :
# if m > j and m in d[i]:
# for p in d[m] :
# if p > m and p in d[j] :
# testSolution(d, [i,j,m,p])
def euler60(upper) :
import primes
import collections
print "Testing up to %d" % upper
p = primes.primes(upper)
d = collections.defaultdict(list)
base = 10
for i in xrange(1, len(p)) :
pi = p[i]
while pi > base : base *= 10
print >> sys.stderr, pi, len(d), "\r",
base2 = base
for j in xrange(i+1, len(p)) :
pj = p[j]
while pj > base2 : base2 *= 10
pipj = pi*base2 + pj
if primes.isPrime(pipj) :
pjpi = pj*base + pi
if primes.isPrime(pjpi) :
d[pi] += [pj]
d[pj] += [pi]
testAllKeys(d)
if __name__ == "__main__" :
euler60(8400)