In Lua, how can I tell if a number divides evenly into another number? i.e with no remainder? I'm just looking for a boolean true or false.
12/6 = 2 (true)
18/6 = 3 (true)
20/6 = 3.(3) (false)
Compare the remainder of the division to zero, like this:
12 % 6 == 0
18 % 6 == 0
20 % 6 ~= 0
The modulus operator (%
) returns the remainder of division. For 12 and 6 it is 0, but for 20 and 6 it is 2.
The formula it uses is: a % b == a - math.floor(a/b)*b