
Let m and n be two integers, 2 ≤ m < n ≤ 10000000. Consider the following set:
Prime(m, n) = { p | p prime and m ≤ p ≤ n }.
Compute the cardinal of the set Prime(m, n) .
The input file consists of several tests. The input of each test is represented on a single line in the input file. Any two consecutive tests are separated by an empty line. For each test, the values for m and n are given on the same line, separated by exactly one space.
For each test, the result will be written to standard output on a different line (the tests will have the same order as in the input file). The results of any two consecutive tests will be separated by an empty line. For each test, the result will be the cardinal of the set Prime(m, n).
예제 입력 1
2 20
70 110
5 150
예제 출력 1
8
10
33
문제에선 최적화를 썼지만 혹시 싶어서 시도해보니 최적화를 안 써도 통과된다. 입출력 주의하는거 외에는 어려운게 없다. 솔직히 이게 왜 실버 1인지는.. 잘모르겠다
import sys
input = sys.stdin.readline
n = 10000000
sieve = [0,0] + [1] * (n+1)
for i in range(2,int(n**0.5)+1) :
if sieve[i] :
for j in range(i*2,n+1,i) : sieve[j] = 0
while 1 :
try :
n , m = map(int,input().split())
input()
cnt = 0
for i in range(n,m+1) :
if sieve[i] :
cnt += 1
print(cnt)
print()
except :
break