利用python,求出1-100之间的素数。
代码如下:
#coding:utf-8
from math import sqrt
N=100
#基本的方法
result = [] #空列表
for num in range(2,N):
f = True # 标记
for snu in range(2, int(sqrt(num))+1):
if num % snu == 0:
f = False
break
if f:
result.append(num)
print result
运行结果:
代码二:
>> result2 = [ p for p in range(2, N) if 0 not in [ p% d for d in range(2, int(sqrt(p))+1)] ]
>>> result2