47 lines
1.1 KiB
Python
47 lines
1.1 KiB
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
from itertools import combinations, product
|
||
|
|
||
|
def primes(n):
|
||
|
sieve = [True] * n
|
||
|
for p in range(2, int(n**0.5)+1):
|
||
|
if not sieve[p]:
|
||
|
continue
|
||
|
for i in range(p**2, n, p):
|
||
|
sieve[i] = False
|
||
|
return [False, False] + sieve[2:]
|
||
|
|
||
|
|
||
|
def is_prime(n):
|
||
|
for p in range(2, int(n**0.5)+1):
|
||
|
if sieve[p] and n%p==0:
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
digs = 10
|
||
|
sieve = primes(10**6)
|
||
|
|
||
|
ts = 0
|
||
|
|
||
|
for dig in range(10):
|
||
|
c = 0
|
||
|
s = 0
|
||
|
num = str(dig) * digs
|
||
|
for rp in range(1,digs):
|
||
|
for comb in combinations(range(digs), rp):
|
||
|
if dig == 0 and comb[0] != 0:
|
||
|
continue
|
||
|
for perm in product(range(10), repeat=rp):
|
||
|
n = num
|
||
|
for i, d in zip(comb, perm):
|
||
|
n = n[:i] + str(d) + n[i+1:]
|
||
|
if is_prime(int(n)) and n[0] != "0":
|
||
|
c += 1
|
||
|
s += int(n)
|
||
|
if c:
|
||
|
break
|
||
|
ts += s
|
||
|
#print("{} {} {:3} {}".format(dig, digs-rp, c, s))
|
||
|
|
||
|
print(ts)
|