Just in case anyone was looking for a decent way to do it…
if (((number/2) - round(number/2)) == 0) return true; return false;
Or whatever the rounding function is in your language of choice.
EDIT: removed unnecessary else.
Modulo operator my dude.
Huh?
return number % 2 == 0
That’s the only sane solution.
Do note how I said “a decent” way, not “the best” way. Get that huh outta here.
Or modulo %
private bool IsEven(int number){ return number % 2 ? false : true; }
number % 2 == 0 and (number & 0b1) == 0
Are the only sane ways to do this. No need to floor. Although If its C and you can’t modulo floats then (number/2 == floor(number/2))
If you are using floats, you really do not want to have an isEven function …
Whats the alternative a macro? An inline function is perfectly fine for checking if a nunber is even. Compiler will probably optimize it to a single and instruction.
No. The alternative is to not use a float. Testing if a float is even simply does not make sense.
Even testing two floats for equality rarely makes sense.
What is the correct output of isEven((.2 + .4) ×10)
Hint: (.2 + .4) x 10 != 6
It does if it dosen’t have a decimal. If it has decimal then it automatically isn’t and the function will return false. Are you talking about cases like 0.1 + 0.2 equaling 0.3000000004 because that is just due to the nature of floats and there is nothing a function can do other than use larger floats for more accuracy.
Take out the
else
and I’m inValid point.
Every bit aside for the ones bit is even. All you have to do is get the ones bit(the far right) for it being a 1 or 0. Which is the fastest and least amount of code needed.
use bitwise &
// n&1 is true, then odd, or !n&1 is true for even return (!(n & 1));
while (true){ if (number == 0) return true; if (number == 1) return false; number -= 2 }
deleted by creator
return !(number % 2)
Setting number to -1 might cause you to wait a while.
You know, shortly after posting this I did think about whether it’s still working if I just pass the underflow that will happen at some point or if I have to fix something in that case (like subtract 1 after the underflow). I deemed it “too complicated” and would just issue a warning that my code is only tested on positive numbers, but I think it will still work.
The number of comments posting a better solution is funny and somewhat concerning.
Yeah, “just use modulo” - no shit, you must be some kind of master programmer
OMG they can’t even!
Oh man, in js we have a package for this magic.
Weekly Downloads: 293.319
I always forget if is even requires is odd or the other way around.
I would never touch js, so idk convention, but this has to be a joke right?
You’d think so but look at the number of active downloads 😅
It looks like this is a handlebars helper.
Handlebars is a temptating language.
I’ve never used handlebars but I’m guessing this is syntactic sugar for non-programmers. Like:
<div>{{if is-even myVariable}} it's even {{else}} it's odd {{endif}}</div>
Looking at their code, it’s really just a bunch of checks to make sure the variable passed is actually an integer that it can work with, followed by the solution:
return (n % 2) === 1;
I can’t think of a more efficient way to get the answer. It does seem like it’d take more time to download the package than to just write the function yourself, though.
Ohh. JS needs you to check the variable during runtime??? That’s… something. I guess that’s what you get for using dynamic typing everywhere. I still bet it’d be faster to do the function by hand though.
left-pad PTSD intensifies
Look at the downloads though!
That one is bad, I use this one https://www.npmjs.com/package/is-is-is-even
Why even do that, just check if this outputs false https://www.npmjs.com/package/is-is-is-is-is-is-odd
Oh fuck, gonna refactor asap!
And it is so light, it only requires is-odd package!
This is your brain on python:
def is_even (num): return num in [x*2 for x in range(sys.maxsize / 2)]
That won’t work tho, you need to make it sys.maxsize//2 to coerce the output into int form
range()
accepts floats, does it not?IIRC it doesn’t; that has caused me pain so many times when trying to generate fractional range
Wow. Amateur hour over here. There’s a much easier way to write this.
A case select:
select(number){ case 1: return false; case 2: return true; }
And so on.
Don’t forget that you can have fall-through cases, so you can simplify it even further:
switch (number) { case 1: case 3: case 5: case 7: case 9: ...
Teach me
Just print True all the time. Half the time it will be correct and the client will be happy, and the other half the time, they will open a ticket that will be marked as duplicate and closed.
Reminds me of the fake thermometers being sold during the peak of COVID that weren’t actually thermometers but just displayed numbers to make people think they were.
I definitely have one of these.
I, EvaX, humbly submit a toast to Nicholas Alexander for successfully managing to pirate WarCraft III so that he may play Defense of the Ancients.
Congratulations Nick. Enjoy your DotA!
(sips from milk goblet)
*cum chalice
There is a simpler way: all the "else if"s can be replaced with simple "if"s
You mean make it even worse.(performance wise)? Interesting.
Please elaborate. When and why would his suggestion be worse?
It will check all of the if statements instead of stopping at the first match.
Each clause already returns
Yeah gotta store the return value in a variable!
My bad, I looked over the returns. If you return something there wouldn’t be much difference between both.
If only you could do something like…
def IsEven(number): return number % 2 == 0
Can’t do that, we are taking about Java.
You’re telling me Java doesn’t support the modulo operator?
Java does have the modulo/remainder support, just like python.
Even the shitposty better version has us:
- take the absolute value of the input as a variable
- while that variable is >1, subtract 2. Repeat until this is no longer true
- if it’s now 1, return true. Otherwise, return false.
On a sidenote, the completely non-shitposty version would be:
return !(var & 1);
Notice the single &, which should be bitwise AND. Fun thing: negative numbers in two’s complement also have 1 for their LSB when odd. C definitely supports bitwise operators, and it appears C# does as well.
You only need to do the comparison on the last digit.
couldn’t you just check the numbers in binary for the ones place
Yeah that should be last bit, not last digit lol.
For another convoluted impl you could do some kind of string format print into a string. Then do your if/else comparing that string containing the last digit. Maybe create a hash set collection of all single digit even strings, then you’ve got O(1) performance using a contains() method.
Modulus equals zero anyone?
That’s the joke
deleted by creator
but that operation is expensive
So what? It works and it’s better than precompiling a list of all known even and odd numbers, and expecting a computer to iterate through a whole list every time it wants to check the value of something.
The stupid trig tables are just as problematic and it’s why graphics chips use other geometric functions instead. It’s just better to use a modulus.
No I’m pretty sure an if else ladder is more better
Nah, it compiles down to an AND because of the constant 2
string taco = variable.ToString()[variable.ToString().Length - 1];
If (taco == “0” || taco == “2” || taco == “4” || taco == “6” || taco == “8”)
return true;
else
return false;
Im something of a coding master myself
as division is complicated and expensive depending on the size of the numbers you’d usually receive as an input, this could be the most efficient solution. Certainly could have the best worst case if we imagine some 128bit shenanigans.
Just realised i fucked up and am checking them as strings instead of chars ¯\_(ツ)_/¯