10以内的素数2,3,5,7的和为17。要求计算得出任意正整数n以内的所有素数的和。

n= int (raw_input())
def find_prime(n):
    L = list(range(2,n + 1))
    m = 0
    while m < len(L):
        n = m + 1
        while n < len(L):
            if L[n]%L[m] == 0:
                del L[n]
            n = n + 1
        m = m + 1
    return L
print( sum (find_prime(n)) )

 

 

n= int(raw_input())
s=2
if n==2:
    print s
else:
    for i in range(n+1):
        j=i-1
        while(j>1):
            if i%j==0:
                break
            else:
                j-=1
                if(j==1):
                    s+=i
    print s

 

【不包含n本身】

import math
n = int (raw_input())
s = sum(range(2,n))
for j in range(2,n):
    for i in range(2, int(math.sqrt(j)) + 1):
      if j % i == 0:
          s=s-j
          break
print s

posted on 2017-07-28 18:27  乾元东  阅读(1443)  评论(0编辑  收藏  举报