Project Euler Problem 049

# Statement

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?

# Solution

It's a brute-force approach with some performance improvements.

from CommonFunctions import find_primes_less_than from itertools import dropwhile is_anagram = lambda x, y: sorted(str(x)) == sorted(str(y)) if __name__ == '__main__': primes = find_primes_less_than(10000) primes_greater_1000 = dropwhile(lambda x: x <= 1000, primes) primes = set(primes) found_one = False for base in primes_greater_1000: for increment in range(1, ((10000 - base) // 2)): n1 = base + increment n2 = base + increment * 2 if n1 in primes and n2 in primes and is_anagram(base, n1) and is_anagram(base, n2): if found_one: print("The result is:", str(base) + str(n1) + str(n2)) exit(0) found_one = True

The Python file is available for download here.