This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Heisenbugs's profile picture

[LUA] Detecting if a variable is a multiple of a number

Started by Heisenbugs, 11 January 2013 - 03:49 PM
Heisenbugs #1
Posted 11 January 2013 - 04:49 PM
Hi there, one more question. Is it possible to detect whether or not a variable is a multiple of a certain number. I want my turtle to place a torch every X blocks, and I can see no way to implement this using a loop (the program is too complex). Each time the turtle goes forward one block in the main shaft the variable D increments by one, so I'm wondering if I can do something like

if D is multiple of X then
for i = 1, 2, 1 do
turtle.turnLeft()
end
turtle.select(torch slot)
turtle.place()
for t = 1, 2, 1 do
turtle.turnLeft()
end
end

Thanks for any feedback.
theoriginalbit #2
Posted 11 January 2013 - 05:06 PM
yes there is… use the modulo / modulus operator %


if i % x == 0 then
  placeTorch()
end
Heisenbugs #3
Posted 11 January 2013 - 06:33 PM
Awesome, thanks.