maxx = 10 def mkp(x): P = [2] p = [2] n = 3 while n < x: for i in p: if n % i == 0: break else: P.append(n) n += 2 while n > p[-1] ** 2: p.append(P[len(p)]) return P prime = mkp(100000) def isp(x, lis = prime): for i in lis: if i ** 2 > x: break if x % i == 0: return False return True maxx = 10 total = 0 for i in xrange(1, 10): for j in xrange(1, 10): num = ['0'] * maxx num[0] = str(i) num[-1] = str(j) tmp = int(''.join(num)) if isp(tmp) and (tmp > 10 ** (maxx - 1)): #print tmp total += tmp for same in xrange(1, 10): for new in xrange(10): for local in xrange(maxx): num = [str(same)] * maxx num[local] = str(new) tmp = int(''.join(num)) if isp(tmp) and (tmp > 10 ** (maxx - 1)): #print tmp total += tmp sp = set([]) no = set(range(10)) for special in [2, 8]: for new1 in no - set([special]): for new2 in no - set([special]): for local1 in xrange(maxx): for local2 in xrange(maxx): num = [str(special)] * maxx num[local1] = str(new1) num[local2] = str(new2) tmp = int(''.join(num)) if isp(tmp) and (tmp > 10 ** (maxx - 1)): sp.add(tmp) #print tmp print total + sum(list(sp))