python中如何编程求1到100之间的素数

Python019

python中如何编程求1到100之间的素数,第1张

1、新建python文件,testprimenum.py;

2、编写python代码,求1到100之间的素数

list1 = []

i = 2

for i in range(2,101):

j = 2

for j in range (2,i):

if i%j == 0:

break

else:

list1.append(i)

print(list1)

3、窗口中右击,选择‘在终端中运行Python文件’;

4、查看执行结果,1-100之间的素数为:

[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]

运用python的数学函数,

先导入math模块

再定义isPrime()方法即可;

使用for进行单行程序扫描素数即可;

运用python的itertools模块判断即可;使用if...while语句来判断即可。