How can I rewrite the solution without using loops?

Problem from the Olympiad 8th grade

There is n kg of metal alloy. It is used to make billets with a mass of K kg each. After that, parts weighing M kg each are turned out of each blank (the maximum possible number of parts is turned out of each blank).

If something remains of the workpieces after this, then this material is returned to the beginning of the production cycle and fused with what is left in the manufacture of the workpieces. If the alloy is the same, which turned out to be enough for the production of at least one blank, then blanks are made from it again, parts are made from them, etc.

Write a program that calculates how many parts can be obtained using this technology from the N kg of alloy available initially.

The program receives three natural numbers N, K, M.

Output one number - the number of parts that can be obtained using this technology.

Forbidden to use cycles(

I could only write using a loop:

N = int(input())
K = int(input())
M = int(input())

count_d = 0

while N >= K:
    count_z = N // K
    count_d += count_z * (K // M)
    N -= count_z * (K // M) * M

print(count_d)
Входные данные: 10 5 2

Выходные: 4
Входные данные: 13 5 3

Выходные: 3
Входные данные: 14 5 3

Выходные: 4
Входные данные: 13 9 4

Выходные: 2
Author: Kromster, 2020-08-01

2 answers

N = int(input())
K = int(input())
M = int(input())

q = K // M
M *= q

print(((N - K + 1) // M + bool((N - K + 1) % M)) * q)

 5
Author: rew, 2020-08-01 14:28:36

Well, redo the loop for recursion (which by the way, in practice, you do not need to do, but even on the contrary, you need to redo the recursion into a loop):

N = int(input())
K = int(input())
M = int(input())

count_d = 0

def f(n):
    global N, K, M, count_d
    if not (n >= K):
        return
    count_z = N // K
    count_d += count_z * (K // M)
    N -= count_z * (K // M) * M
    f(N)
f(N)
print(count_d)

P.S. By the way, you have a very good python. Especially for an eighth grader. Kst code that I wrote is worse because it uses more stack space and still uses globals. But-without cycles.

 3
Author: Victor VosMottor, 2020-08-01 13:47:55