Arithmetic progressions 1487, 4817, 8147 with a term difference of 3330 have two unusual properties:
(i) Each of the three terms is a prime number. (ii) Each term is represented by a permutation of another term. There is no sequence with this property for 1, 2 and 3 digit prime numbers, but there is another for the 4 digit increment sequence.
Now, find the 12-digit number that concatenates the three terms of this sequence. http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%2049
Click here for mymath https://github.com/cof01/ProjectEuler/blob/master/mymath.py
# coding: utf-8
import math
from mymath import get_primes,is_prime
def count_numbers(n):
ret = {i:0 for i in range(10)}
for s in str(n):
ret[int(s)] += 1
return ret
def check(p,diff,pri):
p1, p2, p3 = p, p+diff, p+diff*2
if not is_prime(p2, pri):
return False
if not is_prime(p3, pri):
return False
if count_numbers(p1) == count_numbers(p2) == count_numbers(p3):
return True
else:
return False
def main():
MIN, MAX = 10**3, 10**4
pri = get_primes(MAX)
ans = []
for p in pri['list']:
if p <= MIN:
continue
for diff in range(2, (MAX-p)//2, 2):
if check(p,diff,pri):
ans.append([p, p+diff, p+diff*2])
break
print ans
main()
Recommended Posts