Summation of primes

时间:2014-05-09 16:20:08   收藏:0   阅读:295

是我算法不对,还是笔记本CPU太差?

我优化了两次,还是花了三四个小时来得到结果。

在输出上加1就是最终结果。

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

bubuko.com,布布扣
def isprime(n):
    boolisprime = True
    for i in xrange(3,n):
        if n % i == 0:
            boolisprime = False
            break
    return boolisprime

def primenumber(n):
    i = 1
    sumprime = 0
    while True:
        if isprime(i) == True:
            sumprime += i
            print i,sumprime
        if i > n:
            break
        i += 2
    return sumprime
print primenumber(2000000)+1
bubuko.com,布布扣

 

bubuko.com,布布扣

Summation of primes,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!