Python Exercise Example 12
**Question:** Determine how many prime numbers there are between 101 and 200, and output all of them.
**Program analysis:** Method for judging a prime number: divide the number by each integer from 2 to sqrt(the number). If it is divisible, the number is not prime; otherwise, it is prime.
Program source code:
Example (Python 2.0+)
#!/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
:
(
'
%-4d
'
%
m
)
h
+=
1
if
h
%
10
==
0
:
(
'
'
)
leap
=
1
(
'
The total is %d
'
%
h
)
The output of the above example is:
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 100 Examples