4 points
permalink
report
reply
23 points
*

I know how to fix this!

bool IsEven(int number) {
    bool even = true;
    for (int i = 0; i < number; ++i) {
        if (even == true) {
            even = false;
        }
        else if (even == false) {
            even = true;
        }
        else {
            throw RuntimeException("Could not determine whether even is true or false.");
        }
    }

    if (even == true) {
        return even ? true : false;
    }
    else if (even == false) {
        return (!even) ? false : true;
    }
    else {
        throw RuntimeException("Could not determine whether even is true or false.");
    }
}
permalink
report
reply
7 points

Have you tried seeing if the recursive approach runs faster?

permalink
report
parent
reply
13 points

I know an even better way. We can make it run in O(1) by using a lookup table. We only need to store 2^64 booleans in an array first.

permalink
report
parent
reply

modulo

pseudocode:

if number % 2 == 0
  return "number is even" (is_num_even = 1 or true)
else
  return "number is odd" (is_num_even = 0 or false)

plus you’d want an input validation beforehand

permalink
report
reply
23 points
*

who needs modulo when you can get less characters out of

while (number > 1) {
  number -= 2;
}
return number;

very efficient

edit: or theres the trusty iseven api

permalink
report
parent
reply
9 points

here is somewhat less:

return (number % 2) == 0;

permalink
report
parent
reply
10 points

return !(number & 1);

permalink
report
parent
reply
4 points

just check the last bit jesus christ, what is it with these expensive modulo operations?!

return !(n&1);

permalink
report
parent
reply
2 points

are the negative numbers all even?

permalink
report
parent
reply
2 points

Yes

permalink
report
parent
reply
7 points

🤦

permalink
report
parent
reply
3 points

Name doesn’t check out.

permalink
report
parent
reply
2 points

are u a wizard?

permalink
report
parent
reply
2 points

John carmak posting

permalink
report
parent
reply
4 points
#You are an input. You have value! You matter!
if number % 2 == 0
  return "number is even" (is_num_even = 1 or true)
else
  return "number is odd" (is_num_even = 0 or false)

Am I doing it right? /S.

permalink
report
parent
reply
5 points

Don’t put nbsps in code blocks, they show up literally.

permalink
report
parent
reply
2 points

This code is terrible. If you input 10.66 it returns "number is odd

It should be:

if number % 2 == 0
  return "number is even" (is_num_even = 1 or true)
else
  return "number is not even" (is_num_even = 0 or false)
permalink
report
parent
reply
1 point

Modulo

permalink
report
reply
6 points

print(theJoke % you);

wooosh

permalink
report
parent
reply
3 points

I’m more of a Gemini myself

permalink
report
parent
reply
7 points
Deleted by creator
permalink
report
reply

Programming Horror

!programming_horror@programming.dev

Create post

Welcome to Programming Horror!

This is a place to share strange or terrible code you come across.

For more general memes about programming there’s also Programmer Humor.

Looking for mods. If youre interested in moderating the community feel free to dm @Ategon@programming.dev

Rules

  • Keep content in english
  • No advertisements (this includes both code in advertisements and advertisement in posts)
  • No generated code (a person has to have made it)

Credits

  • Icon base made by Lorc under CC BY 3.0 with modifications to add a gradient

Community stats

  • 327

    Monthly active users

  • 32

    Posts

  • 262

    Comments

Community moderators