美文网首页
Python 练习实例12

Python 练习实例12

作者: loinue | 来源:发表于2022-03-20 07:20 被阅读0次

来自菜鸟教程
https://www.runoob.com/python/python-exercise-example12.html
题目:判断101-200之间有多少个素数,并输出所有素数。

程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。

程序源代码:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
h = 0
leap = 1
from math import sqrt
from sys import stdout
for m in range(101,201):
    k = int(sqrt(m + 1))
    for i in range(2,k + 1):
        if m % i == 0:
            leap = 0
            break
    if leap == 1:
        print ('%-4d' % m)
        h += 1
        if h % 10 == 0:
            print ('')
    leap = 1
print ('The total is %d' % h)

以上实例输出结果为:

101
103
107
109
113
127
131
137
139
149

151
157
163
167
173
179
181
191
193
197

199
The total is 21

相关文章

网友评论

      本文标题:Python 练习实例12

      本文链接:https://www.haomeiwen.com/subject/dnvtdrtx.html