Advertisement
danchaofan

Euler #51

Dec 18th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. from itertools import combinations
  2.  
  3.  
  4. def replace(n):
  5.     n, original = str(n), str(n)
  6.     indexes = []
  7.     for w in range(len(n)-1):
  8.         indexes.append(w)
  9.     combos = list(combinations(indexes, 3))
  10.     for x in combos:
  11.         if not n[x[0]] == n[x[1]] == n[x[2]]:
  12.             continue
  13.         else:
  14.             total = 0
  15.             nums = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  16.             temp = []
  17.             for y in n:
  18.                 temp.append(y)
  19.             for z in nums:
  20.                 temp[x[0]] = str(z)
  21.                 temp[x[1]] = str(z)
  22.                 temp[x[2]] = str(z)
  23.                 check = int("".join(temp))
  24.                 if len(str(check)) < len(temp):
  25.                     continue
  26.                 if check in primes:
  27.                     total += 1
  28.             print(total)
  29.             if total == 8:
  30.                 print(original)
  31.                 quit()
  32.  
  33.  
  34. def primes_sieve(limit):
  35.     a = [True] * limit
  36.     a[0] = a[1] = False
  37.  
  38.     for (i, isprime) in enumerate(a):
  39.         if isprime:
  40.             yield i
  41.             for n in range(i*i, limit, i):
  42.                 a[n] = False
  43.  
  44. primes = list(primes_sieve(10**8))
  45. for b in primes:
  46.     print(b)
  47.     if b < 1000:
  48.         continue
  49.     replace(b)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement