Task 22 Two Printers problem

Back to General discussions forum

Kevinthelopes     2023-08-15 16:26:36

Hello all,

I was wondering about how to optimize the code I have currently for this problem. It works fine for smaller examples but really chugs on the larger examples which is why I'm unable to 'solve it'. I'm aware that this is a 'brute force' solution which is why It's chugging too much on the later example, but I don't quite get how I can use a "least common multiple" or greatest common divisor as is suggested to solve this problem here's a code excerpt to show what I mean

arguments = [1, 1, 5, 3, 5, 4, 105420, 38792, 5804]
for i in range(0,3):
ratePrinter1,ratePrinter2,numberPages=arguments[0],arguments[1],arguments[2]
for i in range(0,3):
    arguments.pop(0)
timecount=1
while True:
    if(timecount%ratePrinter1==0 and numberPages>0):
        numberPages-=1
    if(timecount%ratePrinter2==0and numberPages>0):
        numberPages-=1
    if (numberPages == 0):
        print(timecount, " ")
        break
    timecount+=1
Kevinthelopes     2023-08-15 18:15:21

I apologize if this is too much code for me to post here.

zelevin     2023-08-15 18:32:06

Hint one: note that in some conditions numberPages can get negative, meaning your loop won't ever terminate.

Hint two: do you need to add one to timecount, or is there a bigger number that would speed up your algorithm while leaving it essentially unchanged?

Please login and solve 5 problems to be able to post at forum