• + 0 comments

    def generate_sieve(): constraint = pow(10, 4) boolean_sieve = [True] * (constraint + 1) boolean_sieve[0] = boolean_sieve[1] = False sieve = []

    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