本帖最后由 jerryxjr1220 于 2017-10-9 11:28 编辑
121313
Execution took 0.761506 seconds
import math
import time
def primes1(n):
""" Returns a list of primes < n """
sieve = [True] * int(n/2)
for i in range(3,int(n**0.5)+1,2):
if sieve[int(i/2)]:
sieve[int(i*i/2):n:i] = [False] * int((n-i*i-1)/(2*i)+1)
return [2] + [2*i+1 for i in range(1,int(n/2)) if sieve[i]]
def is_prime(x):
up_bound = int(math.floor(math.sqrt(x)))
for i in range(2,up_bound+1):
if x % i == 0:
return False
return True
start = time.time()
primes = primes1(1000000)
primes = [prime for prime in primes if prime > 100000 and len(set(str(prime))) + 1 < len(str(prime))]
found = False
for prime in primes:
for i in range(0,len(set(str(prime)))):
if found == True:
break
if sorted(str(prime))[i] == sorted(set(str(prime))):
continue
else:
if int(sorted(str(prime))[i]) > 2:
break
elif int(sorted(str(prime))[i]) == 1:
man_prime = str(prime)
man_list = list(man_prime)
count = 1
for j in range(2,10):
indices = [i for i, x in enumerate(str(prime)) if x == '1']
for k in range(0,len(man_prime)):
if k in indices:
man_list[k] = str(j)
man_prime = "".join(man_list)
if is_prime(int(man_prime)):
count += 1
else:
pass
if count >= 8:
print (prime)
found = True
else:
man_prime = str(prime)
man_list = list(man_prime)
count = 1
for j in range(1,10):
indices = [i for i, x in enumerate(str(prime)) if x == '1']
for k in range(0,len(man_prime)):
if k in indices:
man_list[k] = str(j)
man_prime = "".join(man_list)
if is_prime(int(man_prime)):
count += 1
else:
pass
if count >= 8:
print (prime)
break
elapsed = time.time() -start
print ("Execution took {:5f} seconds".format(elapsed))
from itertools import combinations
def isprime(n):
if n%2==0: return False
for i in range(3,int(n**0.5+1),2):
if n%i==0: return False
return True
def gen_replaced_num(snum,n=2):
M = []
for c in combinations(range(len(snum)),n):
slst = list(snum)
m = []
for num in range(10):
ss = slst.copy()
for i in c:
ss[i] = str(num)
if ss[0] == '0': continue
m.append(int(''.join(ss)))
M.append(m)
return M
for i in range(100000,1000000):
for eachgroup in gen_replaced_num(str(i),3):
if sum(1 for each in eachgroup if isprime(each))>=8:
print (eachgroup[0])
exit()
|