-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCountSemiprimes.py
46 lines (34 loc) · 1.34 KB
/
CountSemiprimes.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
43
44
45
46
# link: https://codility.com/demo/take-sample-test/count_semiprimes
# name: Count Semiprimes
__author__ = 'mislav'
def solution(N, P, Q):
# write your code in Python 2.7
primes_num = N
sieve = [0]*(primes_num+2)
prev_prim = [0] *(primes_num+2)
next_prim = [0] *(primes_num+2)
for index in xrange(2,primes_num+1):
prev_prim[index] = index - 1
next_prim[index] = index + 1
current_out = 2
while current_out * current_out <= primes_num:
current_in = prev_prim[primes_num/current_out+1]
while current_in >= current_out:
sieve[current_in*current_out] = current_out
next_prim[prev_prim[current_in*current_out]] = next_prim[current_in*current_out]
prev_prim[next_prim[current_in*current_out]] = prev_prim[current_in*current_out]
current_in = prev_prim[current_in]
current_out = next_prim[current_out]
semiprimes = [0]*(N+1)
for index in xrange(2,N+1):
if sieve[index] == 0:
continue
div_index = index/sieve[index]
if sieve[div_index] == 0:
semiprimes[index] = 1
for index in xrange(1,len(semiprimes)):
semiprimes[index] += semiprimes[index-1]
result = []
for index in xrange(len(P)):
result.append(semiprimes[Q[index]] - semiprimes[P[index]-1])
return result