-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path51.py
36 lines (29 loc) · 907 Bytes
/
51.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
import itertools
def is_prime(n):
if n == 0 or n == 1: return False
if n == 2: return True
if n == 3: return True
i = 2
while i <= n**0.5:
if not n%i: return False
i += 1
return True
N = 10**6
PRIMES = [p for p in xrange(N) if is_prime(p)]
print "Primes generated"
def repeating_digits(n):
# do not allow the repeating digit in end position
s = str(n)
return set([c for c in s if s.count(c) == 3 and s[-1] != c])
def problem51(n=8, c=0):
for p in PRIMES:
for d in repeating_digits(p):
if not c or str(p).count(d) == c:
count = 0
for i in range(10):
q = int(str(p).replace(d, str(i)))
if len(str(q)) == len(str(p)) and q in PRIMES:
count += 1
if count == n:
return p
print problem51()