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

even and odd numbers?

Started by ChiknNuggets, 30 December 2012 - 05:14 AM
ChiknNuggets #1
Posted 30 December 2012 - 06:14 AM
so i got a table and it goes something like this table = {time,value,time,value,time,value,…}
and what i need is a way so that it goes something like this

if odd number sleep(time)
if even number note(value)

but i cant seem to figure out a way to check if the current number is a odd or even number, in something like this

for i=1, #table do


end

Any help is much appreciated
remiX #2
Posted 30 December 2012 - 06:23 AM
You can use math.fmod()

t_table = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

for i = 1, #t_table do
    if t_table[i] % 2 == 0 then -- This is another way of using math.fmod, it's the same as 'if math.fmod(t_table[i], 2) == 0 then'
        print(t_table[i] .. " is even!")
    else
        print(t_table[i] .. " is odd!")
    end
end
ChiknNuggets #3
Posted 30 December 2012 - 06:23 AM
dw found it



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

didnt realise there was a math funtion to check for remainders
Cozzimoto #4
Posted 30 December 2012 - 06:23 AM
use Modulus [%]

its basically finds the remainder of a dividend and returns that remainder.

IE:
1%2 = 1
2%2 = 0
3%2 = 1
4%2 = 0
etc…

so you can use the 0s and 1s to alternate in your table

Modulus is so much fun and makes programming math in technically any programming language easier
ChiknNuggets #5
Posted 30 December 2012 - 06:24 AM
yours does save me the time of changeing this to fit my method, thank you very much
Cozzimoto #6
Posted 30 December 2012 - 06:24 AM
wow we all replied at the same time! =P
GopherAtl #7
Posted 30 December 2012 - 06:24 AM
i%2 will return 0 if even, 1 if odd.

:Edit: wow, 10 minutes of nothing then ninjaspamfest, lol.
Cozzimoto #8
Posted 30 December 2012 - 06:27 AM
thats what i love about this community, help at the ready INSTANTLY

sorry off topic! lol =P
ChiknNuggets #9
Posted 30 December 2012 - 06:40 AM
actually i ran into a problem so while i got ur attention

	   for i = 1, #song do
  if song[i] % 2 == 0 then
   play(song[i])
  else
   sleep(song[i])
	 end
end


it doesnt seem to want to work, as in it skips the parts that would go if it werent in a sleep its really wierd
ChiknNuggets #10
Posted 30 December 2012 - 06:42 AM
dw fixed that too for some reason this works but that doesnt
for i,v in ipairs(song) do
if i % 2 == 0 then
play(v)
else
sleep(v)
end
end
Cozzimoto #11
Posted 30 December 2012 - 06:42 AM
did you try reversing what you got, are you sure you got the right argument types in the functions you are passing through

a modulus returning 0 should be an even number. make sure its a number type though
Cozzimoto #12
Posted 30 December 2012 - 06:43 AM
yea tables are a pain at times if you didnt realize you were trying to compare multiple args to one
ChiknNuggets #13
Posted 30 December 2012 - 06:45 AM
yea idk what it was but now ive finished a touch screen piano that can record songs and play them back at the exact same speed at which you recorded it at