Whats the most efficient way to subtract a larger number by a smaller number until its less than or equal to the smaller number

So they way I’m doing it is by looping until its smaller or equal to the smaller number but looping takes too much time for mutliple numbers being effected so is there a more efficient and faster way for this?

local max = 21
local min = 4

repeat task.wait()
    max -= min
until max <= min
print(max)
1 Like

using that loop is efficient, just remove the task.wait() as it unnecessarily slows it down.

1 Like

max -= min * math.floor(max/min)

floor(max/min) is how many times min goes into max.

Pretty sure you can also just do max = 21 % 4.

1 Like
local function subtractTillLessThan(max : number, min : number) : number
	return max - (math.floor(max / min) * min)
end

Which is essentially the same as using the modulo operator.

1 Like