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 = []

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM