python3求n中的所有素数

基本判断思路:在一般领域,对正整数n,如果用2到√n之间的所有整数去除,均无法整除,则n为素数。

 1 import math
 2 l = []
 3 n = int(input('please input a number more than 2:'))
 4 if n == 2:
 5     print('there is no prime less than 2!')
 6 else:
 7     for a in range(2, n):
 8         for b in range(2, int(math.sqrt(a)) + 1):#素数只需要不能整除2-根号自己就可以了。
 9             l.append(a % b)#将所有b遍历的结果加到列表中
10         if 0 not in l:
11             print(a)
12         l = []
原文地址:https://www.cnblogs.com/wxlblogs/p/7131808.html