Problem 49 Prime permutations

2016-11-14 10:22:00   技术   python projecteulor

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?

import math
import itertools

PRIME_LIST = []
result = {}

def is_Prime(number):
    if number < 2:
        return False
    for x in range(2, int(math.sqrt(number)) + 1):
        if number % x == 0:
            return False
    return True

for i in range(1000, 10000):
    if is_Prime(i):
        PRIME_LIST.append(i)

for prime in PRIME_LIST:
    key_list = [eval(i) for i in str(prime)]
    key_list.sort()
    key_str = str(key_list)
    if key_str in result:
        result[key_str].append(prime)
    else:
        result[key_str] = [prime]

for key in result.keys():
    if len(result[key]) < 3:
        result.pop(key)

for key in result.keys():
    for a, b, c in list(itertools.combinations(result[key], 3)):
        if c - b == b - a:
            print a, b, c

原来很理想化把if len(result[key]) < 3:这里弄成了等于,怎么都求不出来

评论已关闭。
评论共