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?

### Sage Solution

There are really only a handful of ways to go about this, and the timing isn’t going to be too substantial. So, I’m simply going to write an in-line Sage code block and execute it. The idea is to get a list of primes between 1,000 and 10,000, sort their digit decompositions, and then (after seeing which primes have the same digit decompositions), see which prime subsets have an arithmetic sequence underlying the distances between them. No too horribly interesting, but the code below does the job.

import time start = time.time() # make a list of primes P = prime_range(1000,10000) # for each prime, make a list of digits, and sort that list L = [p.digits() for p in P] for i in range(len(L)): L[i].sort() L.sort() # make a list of digits that appear for at least 3 primes M = [] for i in range(len(L)-3): if L[i]==L[i+1] and L[i+1]==L[i+2] and L[i+2]==L[i+3]: if L[i] not in M: M.append(L[i]) # N is a list of lists of at least 3 primes with the same 4 digits N = [] for m in M: N.append([]) for p in P: s = p.digits() s.sort() if s in M: r = M.index(s) N[r].append(p) # Use a Sage combinations iterator to examine sets of 3 primes for n in N: X = combinations_iterator(n, 3) for x in X: if x[1]-x[0] == x[2]-x[1]: print x elapsed = time.time() - start print "time elapsed: %s seconds" % elapsed

When executed, we get the following result.

[1487, 4817, 8147] [2969, 6299, 9629] time elapsed: 0.0485339164734 seconds