-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler60.py
83 lines (67 loc) · 2.31 KB
/
euler60.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
from bisect import bisect_left
def sieve(limit):
a = [True] * limit # Initialize the primality list
a[0] = a[1] = False
for (i, isprime) in enumerate(a):
if isprime:
yield i
for n in range(i * i, limit, i): # Mark factors non-prime
a[n] = False
# sqrt(1000000000) = 31622
__primes = list(sieve(31622))
def isPrime(n):
# if prime is already in the list, just pick it
if n <= 31622:
i = bisect_left(__primes, n)
return i != len(__primes) and __primes[i] == n
# Divide by each known prime
limit = int(n ** .5)
for p in __primes:
if p > limit:
return True
if n % p == 0:
return False
# fall back on trial division if n > 1 billion
for f in range(31627, limit, 6): # 31627 is the next prime
if n % f == 0 or n % (f + 4) == 0:
return False
return True
primeList = [x for x in __primes if x < 9000]
# --------------------------------------
# Check if it is concatenatable into prime
# --------------------------------------
def isConcatPrime(p1, p2):
if isPrime(int(str(p1) + str(p2))) and isPrime(int(str(p2) + str(p1))):
return True
return False
# --------------------------------------
# Generate Prime set
# --------------------------------------
primeSet = {}
def generatePrimeSet():
for i in range(len(primeList)):
concatSet = set()
for j in range(i + 1, len(primeList)):
if isConcatPrime(primeList[i], primeList[j]):
concatSet.add(primeList[j])
primeSet[primeList[i]] = concatSet
# --------------------------------------
# Continuous Intersection
# --------------------------------------
def continuousIntersection(path, intersect, depth, minSum=2147483647):
if depth == 0:
return sum(path)
else:
for p in intersect:
s = continuousIntersection(
path + [p], intersect & primeSet[p], depth - 1, minSum)
minSum = min(s, minSum)
return minSum
# --------------------------------------
# Glue all the pieces together
# --------------------------------------
generatePrimeSet()
minSum = 2147483647 # 2^31 - 1
for key in primeSet:
minSum = min(minSum, continuousIntersection([key], primeSet[key], 4))
print(minSum)