1. 程式人生 > >求10000以內所有質數的和

求10000以內所有質數的和

# -*- coding: utf-8 -*-
 
def zhishu(num=None):
    if num is None or num == 1 or num < 0:
        return 0
     
    i = 2
    while True:
        if i == num:
            return num
        elif num % i != 0:
            i += 1
        elif num % i == 0 and num != i:
            break
         
    return 0
 
def sum_zhishu():
    sum = 0
    for i in range(10001):
        sum += zhishu(i)
    return sum
 
print sum_zhishu()

參考:http://baike.baidu.com/link?url=TpvFbbDQbNWtalMjZIc1EZC3288x8I6ki2pyz5UJFtIHx2alz0AfMwWbG-IrnG5qDnbrlS3tBs7t4VinnJomIq