码迷,mamicode.com
首页 > 其他好文 > 详细

WeChall_Prime Factory (Training, Math)

时间:2016-09-04 16:04:31      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

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

 

WeChall_Prime Factory (Training, Math)

标签:

原文地址:http://www.cnblogs.com/zhurb/p/5839441.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!