python求100以内素数

时间:2023-02-23 23:55:08

python求100以内素数之和

from math import sqrt
# 使用isPrime函数
def isPrime(n):
    if n <= 1:
        return False
    for i in range(2, int(sqrt(n)) + 1):
        if n % i == 0:
            return False
    return True

count = 0
for i in range(101):
    if isPrime(i):
        count += i
print(count)

# 单行程序扫描素数
from math import sqrt
N = 100
print(sum([p for p in range(2, N) if 0 not in [p % d for d in range(2, int(sqrt(p)) + 1)]]))