Example #1
0
   # for shortness:
   p = primes
   s = primeSet

   for a in range(lp):
      for b in range(a + 1, lp):
         if any(p[x] + p[y] not in s or p[y] + p[x] not in s for x, y in
               product((a,), (b,))):
            continue

         for c in range(b + 1, lp):
            if any(p[x] + p[y] not in s or p[y] + p[x] not in s for x, y in
                  product((a,b), (c,))):
               continue

            for d in range(c + 1, lp):
               if any(p[x] + p[y] not in s or p[y] + p[x] not in s for x, y in
                     product((a,b,c), (d,))):
                  continue

               for e in range(d + 1, lp):
                  if any(p[x] + p[y] not in s or p[y] + p[x] not in s for x, y in
                        product((a,b,c,d), (e,))):
                     continue

                  print sum(p[i] for i in (a,b,c,d,e))

if __name__ == "__main__":
   primes = [str(p) for p in takewhile(lambda p: p <= kLimit, genprimes(False))]
   calc2(primes)
Example #2
0
   56003
   56113
   56333
   56443
   56663
   56773
   56993
     ^^
"""
from primes import genprimes
from itertools import groupby, combinations, izip

kReplacements = [str(i) for i in range(10)]

if "__main__" == __name__:
   primeStrings = (str(p) for p in genprimes())
   bestSoFar = 0
   for size, primes in groupby(primeStrings, len):
      primes = tuple(primes)
      primeBag = frozenset(primes)
      print "Examining primes of length %d, of which there are %d." % (size,
            len(primes))

      for (p1, p2) in combinations(primes, 2):
         if p1[-1] != p2[-1]:
            # Last digit won't change. Some of the resulting numbers would be
            # even, and thus not prime.
            continue
         score = 0
         misses = 0
         hits = []