-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path051.py
48 lines (42 loc) · 906 Bytes
/
051.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
#Project Euler Problem 51
# ### !!!
# Using the script meusPrimos.py
from meusPrimos import primes
Primes=primes(1000000)
PrimesSet=set(Primes)
SZ=len(Primes)
N=3
FAMILY=8
def checkN(p,N):
pstr=[c for c in str(p)]
for c in pstr:
if pstr.count(c)==N:
return int(c)
return -1
def replace(p,old,new):
pstr=[c for c in str(p)]
if new==0 and int(pstr[0])==old:
return -1 #leading 0's not allowed
for i,c in enumerate(pstr):
if int(c)==old:
pstr[i]=str(new)
v=0
pstr.reverse()
for i in range(len(pstr)):
v+=int(pstr[i])*10**i
return v
start=100000
for p in Primes:
if p<start: continue
d=checkN(p,N)
if d==-1: continue
count=0
for i in range(10):
if replace(p,d,i) in PrimesSet:
count+=1
if count==FAMILY: break
print p
'''
takes about 1s ...
121313
'''