from math import*
n=int (input("请输入n:"))
m=int (sqrt(n))
p=[i for i in range(n+1)]
for i in range(2,m+1):
if p[i]:
for j in range(2*i,n+1,i):
p[j]=0
for i in range(2,n+1):
if p[i]:
print(p[i])
结果:
from math import*
n=int (input("请输入n:"))
m=int (sqrt(n))
p=[i for i in range(n+1)]
for i in range(2,m+1):
if p[i]:
for j in range(2*i,n+1,i):
p[j]=0
for i in range(2,n+1):
if p[i]:
print(p[i])
结果: