首页 > 其他 > 详细

WeChall_Prime Factory (Training, Math)

时间:2016-09-04 16:04:18      阅读:689      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!