嵇爾的吐槽

#没事画轮子的嵇尔不定期的(W)碎(E)碎(B)念(B)和(L)吐(O)槽(G)

Problem 51 Prime digit replacements 2016-11-16 13:39:00

By replacing the 1st digit of the 2-digit number *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.

By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.

Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family. import math import itertools

index = {}


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 digit in range(2, 7):
    check_list = []
    for combin in range(1, digit):
        for i in list(itertools.combinations(range(0, digit), combin)):
            check_list.append(list(i))
    for prime in range(10**(digit - 1), 10**digit):
        if is_Prime(prime):
            for check_items in check_list:
                i_str = str(prime)
                t = []
                for j in range(0, digit):
                    if not j in check_items:
                        t.append(i_str[j])
                if len(list(set(t))) == 1:
                    check_key = ''
                    for j in range(0, digit):
                        if j in check_items:
                            check_key += i_str[j]
                        else:
                            check_key += '*'
                    if check_key in index:
                        index[check_key].append(prime)
                    else:
                        index[check_key] = [prime]

small = 1000000
for key in index.keys():
    if len(index[key]) == 8:
        if index[key][0] < small:
            small = index[key][0]
print small

审题很重要,原来替换的还需要是相同的,这段的判断逻辑比较难看,不过不高兴改了

评论共