##第一种
import math
num = []
i = 2
for i in range(2, 100):
j = 2
for j in range(2, i):
if(i % j == 0):
break
else:
num.append(i)
print(num)
##第二种
import string
def prime(num):
sushu = {}
for i in range(0, num+1):
sushu[0] = False
sushu[1] = False
sushu[2] = True
sushu[3] = True
for j in range(2, i-1):
if(i % j == 0):
sushu[i] = False
break
else:
sushu[i] = True
if sushu[i]:
print(i, end=" ")
prime(100)
?
##第三种
import math
sushu = True
for i in range(2, 100):
sushu = True
for j in range(2, int(math.sqrt(i))+1):
if i % j == 0:
sushu = False
break
if sushu:
print(i, end=' ')
##第五种
for i in range(2, 100):
for j in range(2, i):
if i % j == 0:
break
else:
print(i, end=" ")
?
?
##第六种
def eratosthenes(n):
sushu = [True] * (n + 1)
sushu[1] = False # 1不为素数
for i in range(2, int(n ** 0.5) + 1):
if sushu[i]:
for j in range(i * i, n + 1, i):
sushu[j] = False
return {x for x in range(2, n + 1) if sushu[x]}
if __name__ == "__main__":
print(eratosthenes(100))
?
?
|