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

[Question] how to check if a variable is even or odd

Started by drats666, 03 February 2013 - 05:12 AM
drats666 #1
Posted 03 February 2013 - 06:12 AM
I have a little program i been working on that is designed to dig out a simple tunnel. the program ask for 3 variables width, length, sleep. while making a change to my code to prepare for a new function i was going to add i ran into an issue. after messing with the code for an hour i decided to post after doing a few google checks.
i need to take a number and check if it is even or odd, however i actually have no idea how to do that in lua. after searching i found for lua itself a solution to use math.mod but it seems that isn't in computercraft or not enabled by default.

here is a sumocode of what i want to do
print enter width
width = userinput
print enter length
length = userinput
if x is even
	turn right
	move back xwidth
	turn right
	move back xlength
end
if x is odd
	turn left
	move back xwidth
	turn left
	move back xlength
end
dissy #2
Posted 03 February 2013 - 06:19 AM

if math.mod(x, 2) == 0 then
print("x is even")
else
print("x is odd")
end


There is also the "%" command which works the same way.


if (x % 2) == 0 then
  print("x is even")
else
  print("x is odd")
end

edit: oops, lost half that second function
ikke009 #3
Posted 03 February 2013 - 06:20 AM
or
if x % 2 == 0 then
tesla1889 #4
Posted 03 February 2013 - 06:21 AM

--snip--

for faster code:

if ((x % 2) == 0) then
print("x is even")
else
print("x is odd")
end

EDIT: lol ikke009 beat me to it
dissy #5
Posted 03 February 2013 - 06:29 AM
for faster code:

So now I'm very curious, as this is like the 3rd time someone posted the same reply.

How/why is "if ((x % 2) == 0) then" faster than "if ( x % 2) == 0 then" ?

Edit: Note I'm not arguing it isn't, I just don't understand how adding more ()'s would make it faster :}
tesla1889 #6
Posted 03 February 2013 - 06:45 AM
–snip–

i wasnt

i was saying that

x % 2
is faster than

math.mod(x,2)
drats666 #7
Posted 03 February 2013 - 07:17 AM
thanks to the quick and wonderful help here i was able to resolve my issue and learn something in the process :-p my turtle now when done mining returns home to a chest that was placed beside it and unloads its cargo. it now places torches as it goes along mining
ikke009 #8
Posted 03 February 2013 - 07:42 AM
wonderful job mate. in case you didn't know, the % sign (called modulo) gives you the result of a division stripped of all integers.
so 1 % 4 and 5 % 4 would both yield 0.25
MysticT #9
Posted 03 February 2013 - 10:04 AM
wonderful job mate. in case you didn't know, the % sign (called modulo) gives you the result of a division stripped of all integers.
so 1 % 4 and 5 % 4 would both yield 0.25
err, not really. It returns the raminder of the integer division. Both 1 % 4 and 5 % 4 would return 1.
tesla1889 #10
Posted 03 February 2013 - 10:05 AM
EDIT: whoops, MysticT and i had the same thought at the same time