forked from gjw199513/python100example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample12.py
More file actions
36 lines (32 loc) · 855 Bytes
/
example12.py
File metadata and controls
36 lines (32 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# -*- coding:utf-8 -*-
__author__ = 'gjw'
__time__ = '2018/1/4 0004 下午 5:14'
# 题目:判断101-200之间有多少个素数,并输出所有素数。
# 程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,则表明此数不是素数,反之是素数。
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("{0:-4d}".format(m))
h += 1
leap = 1
print("The total is"+str(h))
# 集合法
print("我是新方法")
l = []
for i in range(101, 200):
k = int(sqrt(i+1))
for j in range(2, k):
if i%j == 0:
break
else:
l.append(i)
print(l)
print("总数为:"+str(len(l)))