We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
for i in range(len(boolean_sieve))[2:]:
if boolean_sieve[i]:
for j in range(len(boolean_sieve))[i*2::i]:
boolean_sieve[j] = False
if i * i >= constraint:
break
for i in range(len(boolean_sieve)):
if boolean_sieve[i]:
sieve.append(i)
return sieve
def waiter(number, q):
answers = []
sieve = generate_sieve()
A = number
for i in range(q):
next_A = []
B = []
while A:
plate = A.pop()
if plate % sieve[i] == 0:
B.insert(0, plate)
else:
next_A.append(plate)
answers.extend(B)
A = next_A
answers.extend(reversed(A))
return answers
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Waiter
You are viewing a single comment's thread. Return to all comments →
def generate_sieve(): constraint = pow(10, 4) boolean_sieve = [True] * (constraint + 1) boolean_sieve[0] = boolean_sieve[1] = False sieve = []
def waiter(number, q): answers = [] sieve = generate_sieve() A = number