Jump to content




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


  • You cannot reply to this topic
9 replies to this topic

#1 drats666

  • Members
  • 7 posts

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


#2 dissy

  • Members
  • 181 posts

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

#3 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

Posted 03 February 2013 - 06:20 AM

or
if x % 2 == 0 then

#4 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 03 February 2013 - 06:21 AM

View Postdissy, on 03 February 2013 - 06:19 AM, said:

--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

#5 dissy

  • Members
  • 181 posts

Posted 03 February 2013 - 06:29 AM

View Posttesla1889, on 03 February 2013 - 06:21 AM, said:

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 :}

#6 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 03 February 2013 - 06:45 AM

View Postdissy, on 03 February 2013 - 06:29 AM, said:

--snip--

i wasnt

i was saying that
x % 2
is faster than
math.mod(x,2)


#7 drats666

  • Members
  • 7 posts

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

#8 ikke009

  • Members
  • 224 posts
  • LocationSliding between sunbeams

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

#9 MysticT

    Lua Wizard

  • Members
  • 1,597 posts

Posted 03 February 2013 - 10:04 AM

View Postikke009, on 03 February 2013 - 07:42 AM, said:

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.

#10 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 03 February 2013 - 10:05 AM

EDIT: whoops, MysticT and i had the same thought at the same time





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users