#求100以内的素数并输出
def isPrime(num):
for i in range(2,num):
if num%i == 0:
return False
return True
sum = 2#1不是素数,2是素数
#对[3,100)内的整数逐一进行判断
for i in range(3,100):
if isPrime(i):
sum += i
print(sum)
优化版:
from math import sqrt
i = 2
while i <= 100:
j = 2
while j <= sqrt(i):
if i%j == 0:
break
j += 1
if j > sqrt(i):
print(i,end = ' ')
i += 1
from math import sqrt
for i in range(2,101):
k = int(sqrt(i))
flag = 1
for j in range(2,k+1):
if i%j == 0:
flag = 0
break
if(flag):
print(i,end = " ")