-
Notifications
You must be signed in to change notification settings - Fork 0
/
euler51.py
55 lines (45 loc) · 1.68 KB
/
euler51.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
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 range(100, 10 ** 6) if isPrime(x))
def testPrimes(testPrimes, searchLen=8):
for prime in testPrimes:
primeStr = str(prime)
uniqueChars = set(list(primeStr))
for uniqueChar in uniqueChars:
primeList = []
for replacementChar in range(ord('0'), ord('9') + 1):
replaced = int(primeStr.replace(
uniqueChar, chr(replacementChar)))
# second check needed because of trailing zeros
if isPrime(replaced) and len(str(replaced)) == len(primeStr):
primeList.append(replaced)
if len(primeList) == searchLen:
print(primeList)
return min(primeList)
print(testPrimes(primeList))