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?
from time import time
def sieve(n):
"""generates primes up to n."""
s = [True] * (n + 1)
for p in range(2, n):
if s[p]:
yield p
for i in range(p * p, n, p):
s[i] = False
def is_permutation(n1, n2):
"""returns True if n1 is permutation of n2"""
to_str_1 = str(n1)
to_str_2 = str(n2)
if n1 == n2:
return False
to_str_1_digits = {digit: to_str_1.count(digit) for digit in to_str_1}
to_str_2_digits = {digit: to_str_2.count(digit) for digit in to_str_2}
if not to_str_1_digits == to_str_2_digits:
return False
return True
def get_permutations(n):
"""generates tuples of 3 permutations each within range n."""
primes = set(sieve(n))
for prime1 in primes:
for prime2 in primes:
if is_permutation(prime1, prime2):
for prime3 in primes:
if is_permutation(prime3, prime1) and is_permutation(prime3, prime2):
yield prime1, prime2, prime3
def check_subtraction(n):
"""checks permutations within range n for subtraction rules.
returns valid permutations."""
permutations = get_permutations(n)
for x, y, z in permutations:
if abs(x - y) == abs(y - z):
return x, y, z
if __name__ == '__main__':
start_time = time()
x, y, z = sorted(check_subtraction(10000))
print(str(x) + str(y) + str(z))
print(f'Time: {time() - start_time} seconds.')