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?
In [1]:
def primes_less_than_n(n):
""" Returns a list of primes < n """
sieve = [True] * n
for i in xrange(3,int(n**0.5)+1,2):
if sieve[i]:
sieve[i*i::2*i]=[False]*((n-i*i-1)/(2*i)+1)
return [2] + [i for i in xrange(3,n,2) if sieve[i]]
max_num_digits = 4
primes = primes_less_than_n((10**max_num_digits+1)-1)
primes = [p for p in primes if p > 10**3]
print len(primes)
In [2]:
import itertools
def is_arith_seq(s):
if len(s) <= 2:
return False
d = s[1] - s[0]
for i in range(len(s)-1):
if s[i+1] - s[i] != d:
return False
return True
for p in primes:
perms = []
for pe in set(itertools.permutations(str(p))):
t = int(''.join(pe))
if t in primes:
perms.append(t)
for pe in itertools.combinations(perms, 3):
pes = sorted(pe)
if is_arith_seq(pes):
print pes