Jump to content




how do you tell if a number is even or odd


28 replies to this topic

#1 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 25 March 2013 - 12:35 PM

title says it all

#2 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 25 March 2013 - 12:41 PM

you can use division.......

#3 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 25 March 2013 - 12:44 PM

in java, i know you can do this

var Num = 4

if (Num % 2 === Num/2)
{
Number is even
}
else
{
Number is odd
}
its java though (tinn57, syntax?)

#4 ETHANATOR360

  • Members
  • 423 posts
  • Locationyour hardrive

Posted 25 March 2013 - 12:55 PM

i know java im asuming mod symbol works in lua

#5 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 12:58 PM

Bam!

Read on!

#6 alakazard12

  • Banned
  • 93 posts

Posted 25 March 2013 - 01:07 PM

Here's a simple function for you:

function GetType(Number)
local Derp = Number % 2
if Derp == 0 then
return "Even"
else
return "Odd"
end
end

#7 Brandhout

  • Members
  • 18 posts

Posted 25 March 2013 - 01:41 PM

Another nice way is to use bitwise operators, every even number has a zero last bit. To check this:

function getType(number)
if bit.band(number,1) == 0 then
  return "Even"
end
return "Odd"
end

Although im not sure how much of a difference this makes in lua.

#8 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 01:46 PM

Either way works. It is up to him what he wants to use. I would rather use the bitwise operator.

#9 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 25 March 2013 - 01:58 PM

It's much easier, way faster, and native to lua to use the modulo operator.

function isEven(num)
  return num % 2 == 0
end


#10 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 01:59 PM

I guess that works as well :P Still a fan of the bitwise operator

#11 Brandhout

  • Members
  • 18 posts

Posted 25 March 2013 - 02:07 PM

Im still new to lua, so as i said i wasn't sure if the bitwise operator had an advantage over the modulo operator. I know that in some languages it definitely does.

#12 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 02:11 PM

In this case, the modulo operator has the bitwise operator beaten. I still like it though ^_^

#13 LuaEclipser

  • Banned
  • 220 posts
  • LocationCleveland, Ohio

Posted 25 March 2013 - 02:26 PM

modulo = java :P

#14 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 02:29 PM

Java = nil :wacko:

#15 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 25 March 2013 - 02:31 PM

View PostSuicidalSTDz, on 25 March 2013 - 02:29 PM, said:

Java = nil :wacko:/>
Java = null :P

#16 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 02:57 PM

View PostEngineer, on 25 March 2013 - 02:31 PM, said:

View PostSuicidalSTDz, on 25 March 2013 - 02:29 PM, said:

Java = nil :wacko:/>
Java = null :P
Lua perspective dear Watson ^_^

If Java had true Garbage Collection, well, there would be no Java..

#17 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 March 2013 - 03:09 PM

there is also a modulo function in the math library...
its math.fmod iirc. because math.modf gets the fraction part of the number.


View PostSuicidalSTDz, on 25 March 2013 - 02:57 PM, said:

View PostEngineer, on 25 March 2013 - 02:31 PM, said:

Java = null :P
Lua perspective dear Watson ^_^
Nope, u gunna talk about Java, you look at it from a Java perspective.

#18 SuicidalSTDz

    Permutator of Strings

  • Members
  • 1,308 posts
  • LocationPennsylvania

Posted 25 March 2013 - 03:14 PM

View PostTheOriginalBIT, on 25 March 2013 - 03:09 PM, said:

Nope, u gunna talk about Java, you look at it from a Java perspective.
But, but, no! Unacceptable!

#19 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 25 March 2013 - 03:15 PM

View PostTheOriginalBIT, on 25 March 2013 - 03:09 PM, said:

because math.modf gets the fraction part of the number.

Wait. Why does modf even exist? It's ridiculously easy to make modf yourself.

function modf(n)
  return n - n%1, n%1
end


#20 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 25 March 2013 - 03:21 PM

View PostKingdaro, on 25 March 2013 - 03:15 PM, said:

Wait. Why does modf even exist? It's ridiculously easy to make modf yourself.
Why do most functions exist... for convenience to the developer not to have to make the function each time... most of the math and string functions can be easily made...





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users