from common import sieve # # WARNING: uses a lot of RAM! # # notes: # - limit:1000000, sieve:50000000 -> no result when starting with ["3"] or ["7"] # - limit:800000, sieve:50000000 -> no result! # - limit:100000, sieve:100000000 -> [['13', '5197', '5701', '6733', '8389']] # # limit = 9000 primes = sieve(int(str(limit)+str(limit))).primes() prime_list = [ p for p in primes if p < limit] prime_list.sort() prime_list_str = [ str(p) for p in prime_list ] def check_list(l): result = [] for sublist in l: for p in prime_list_str: isprime = True for n in sublist: if int(n+p) not in primes or int(p+n) not in primes: isprime = False break if isprime: result += [ sublist+[p] ] return result def cleanup(l): result = [] for sublist in l: sublist.sort() if sublist not in result: result += [sublist] return result l = [ [p] for p in prime_list_str ] for i in range(4): l = cleanup(check_list(l)) print l