def solve(): sieve = Sieve() sieve.compute_upto(100000) # prime the sieve # we're starting with 23 to make life easy found_soln = False primes = (i for i in count(23) if sieve.is_prime(i)) digits = ''.join(str(d) for d in range(0,10)) # 0-9 for p in primes: p_str = str(p) for i in xrange(1,len(p_str)): # combinations of indices into p_str of length i where every value index in # p_str has the same character idx_combs = (j for j in combinations(xrange(0,len(p_str)), i) if all(imap(lambda x: p_str[x] == p_str[j[0]], j)) and len(j) > 0) for comb in idx_combs: group_members = set([]) # we've got all the possible index combinations, so let's try swapping them for new_char in digits: test_str = set_val_at_idxs(p_str, comb, new_char) val = int(test_str) if not test_str.startswith('0') and sieve.is_prime(val): group_members.add(val) if len(group_members) >= 7: print sorted(group_members) if len(group_members) == 8: found_soln = True break; if found_soln: break if found_soln: break