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

Odd or Even

Started by Evan, 18 March 2012 - 10:50 PM
Evan #1
Posted 18 March 2012 - 11:50 PM
Hello,
I'm currently making a Mining Turtle which flattens land out, and the user specifies amount of blocks forward, amount of blocks to the right, and amount of blocks to go up.

I've got it working but I have had to limit the maximum size to the right to 80, as the way I have programmed it I need to whether or not a number is odd.

At the moment I have something similar to this:


odd = {1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,67,69,71,73,75,77,79}
input = readln()
num = tonumber(input)
found = false
for a=1,40 do
    if odd[a] == num then
        found = true
        break
    end
end
if found == true
    print("Odd")
else
    print("Even")
end

I tried using math.mod but it didn't seem to work, and this way is limitted to the size of the array.

So anyone got any suggestions?

Thanks
Evan.
Espen #2
Posted 19 March 2012 - 12:06 AM

local input = read()
local num = tonumber( input )

if ( num % 2 ) == 0 then
  print("Even")
else
  print("Odd")
end
% is the modulo operator.
Edited on 18 March 2012 - 11:06 PM
Evan #3
Posted 19 March 2012 - 10:25 AM
Ah yeah, that worked, thanks for the help :)/>/> Just posted the program I needed it for in the turtle programs library :D/>/>

Cheers man