-
Notifications
You must be signed in to change notification settings - Fork 7
/
count_primes.py
42 lines (36 loc) · 1.12 KB
/
count_primes.py
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
37
38
39
40
41
42
class Solution: # 868 ms
def buildPrefix(self, MAX):
self.prefix =[0]*(MAX + 1)
# Create a boolean array value in
# prime[i] will "prime[0..n]". A
# finally be false if i is Not a
# prime, else true.
prime = [1]*(MAX + 1)
p = 2
while(p * p <= MAX):
# If prime[p] is not changed,
# then it is a prime
if (prime[p] == 1):
# Update all multiples of p
i = p * 2
while(i <= MAX):
prime[i] = 0
i += p
p+=1
# Build prefix array
# prefix[0] = prefix[1] = 0;
for p in range(2,MAX+1):
self.prefix[p] = self.prefix[p - 1]
if (prime[p]==1):
self.prefix[p]+=1
def query(self,L, R):
return self.prefix[R]-self.prefix[L - 1]
def countPrimes(self, n):
"""
:type n: int
:rtype: int
"""
if n <= 1: return 0
else:
self.buildPrefix(n)
return self.query(1,n-1)