WeChall_Prime Factory (Training, Math)

Your task is simple:
Find the first two primes above 1 million, whose separate digit sums are also prime.
As example take 23, which is a prime whose digit sum, 5, is also prime.
The solution is the concatination of the two numbers,
Example: If the first number is 1,234,567
and the second is 8,765,432,
your solution is 12345678765432

 

解题:

素数打表,依次从小到达剔除素数的倍数的数。

def allsum(x):
    sum = 0
    while x:
        sum += x%10
        x //= 10
    return sum

total = 2000000
prime = []
a = [1 for i in range(total)]
for i in range(2,total):
    if a[i]:
        prime.append(i)
        time = 2
        while 1:
            num = time*i
            if num >= total:
                break
            a[num] = 0
            time += 1

find = 0
for i in range(1000000,total):
    if i in prime and allsum(i) in prime:
        print(i)
        find += 1
        if find == 2:
            break

 

posted @ 2016-09-04 15:28  zzzzzzzzhu  阅读(808)  评论(0编辑  收藏  举报