Home>
Question

The calculation of the written code does not end.
What is the problem?
Thank you very much.

A, B, X = map (int, input (). split ())
def N_prices (n):
    return (A * n + B * (len (str (n))))
if X<= N_prices (0):
    print ('0')
if N_prices (10 ** 9)<= X:
    print ('109')
for i in range (1, 10 ** 9):
    if N_prices (i)<= X and X<= N_prices (i + 1):
        print (str (i))
Environment

python3.7.3, jupyter notebook

  • Answer # 1

    for i in range (1, 10 ** 9):
        if N_prices (i)<= X and X<= N_prices (i + 1):
            print (str (i))

    10 ** 9, that is, the above code is repeated 1000 million times, so it will take a lot of time.

  • Answer # 2

    Because I'm trying to calculate 10 ^ 9 times, if it takes 1us to do it once, it's okay to take 1000 seconds.

    Easy estimation.

    $python -m timeit '(12345 * 10111213 + 67890 * (len (str (10111213))))'
    1000000 loops, best of 3: 0.295 usec per loop

  • Answer # 3

    How about reducing the number of loops to see how long it takes?
    for i in range (1, 10 ** 6)
    I think that is the limit that can wait.