''' The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence. What 12-digit number do you form by concatenating the three terms in this sequence? ''' import math import numpy as np from itertools import permutations def sieve(n): assert n > 1 ns = [True] * n for i in range(2, math.ceil(np.sqrt(n))): if ns[i]: j = pow(i, 2) while j < n: ns[j] = False j = j + i return [i for i,val in enumerate(ns) if val][2:] def digitToList(n): return [int(c) for c in str(n)] def listToDigit(l): res = 0 for i in l: res = 10 * res + i return res def main(): print("Hello this is Patrick") primeSet = set(sieve(10000)) - set(sieve(1000)) primeList = list(primeSet) for p in primeList: perms = list(set(map(listToDigit, list(permutations(digitToList(p)))))) for perm in perms: if p != perm and perm in primeSet: if max(p, perm) + abs(p - perm) in primeSet and max(p, perm) + abs(p - perm) in perms: print(min(p, perm), max(p, perm), max(p, perm) + abs(p - perm)) if __name__ == "__main__": main()