64 points

You could do this in one line…

By removing all the linebreaks.

permalink
report
reply
12 points
*
Deleted by creator
permalink
report
parent
reply
2 points

i think it should one giant ternary expression composition

permalink
report
parent
reply
1 point

I love this thread 🫠

permalink
report
parent
reply
45 points

Of course there’s an easier way. Just integrate the state of the art API dedicated for this exact problem. https://isevenapi.xyz/

permalink
report
reply
36 points

This is confusing. I’m already using the iSeven API to determine if a number is 7. I’m getting a namespace collision error when I try to load this new API. Bug report filed.

permalink
report
parent
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
22 points

Back when I was learning programming a lot of lessons would make you do something like this, and then show you the real way to do it in the next lesson. My reaction was always “why didn’t you lead with this?”.

permalink
report
reply
16 points

You must see the pain before you confront it.

permalink
report
parent
reply
7 points

Because the point of the lesson is to demonstrate that you can solve the same problem multiple ways where some paths are more efficient than others.

Bad programmers are the ones that find the first solution and implement it no matter how inefficient it is.

Good programmers spend time on figuring out the solution with the least amount broken or inefficient code. You don’t learn this by jumping straight to the best answer every time.

permalink
report
parent
reply
20 points
*

My solution in perl back in the day when I was a teenage hobbyist who didn’t know about the modulus operator: Divide by 2 and use regex to check for a decimal point.

if ($num / 2 =~ /\./) { return “odd” }
else { return “even” }

permalink
report
reply
22 points

Divide by 2 and check for a decimal point.

I mean, it ain’t wrong.

permalink
report
parent
reply
4 points

You know, I was going to let this slide under the notion that we’re just ignoring the limited precision of floating point numbers… But then I thought about it and it’s probably not right even if you were computing with real numbers! The decimal representation of real numbers isn’t unique, so this could tell me that “2 = 1.9999…” is odd. Maybe your string coercion is guaranteed to return the finite decimal representation, but I think that would be undecidable.

permalink
report
parent
reply
4 points

Ackchyually-- IEEE 754 guarantees any integer with absolute value less than 2^24 to be exactly representable as a single precision float. So, the “divide by 2, check for decimals” should be safe as long as the origin of the number being checked is somewhat reasonable.

permalink
report
parent
reply
1 point

The decimal representation of real numbers isn’t unique, so this could tell me that “2 = 1.9999…” is odd.

I don’t think your belief holds water. By definition an even number, once divided by 2, maps to an integer. In binary representations, this is equivalent to a right shift. You do not get a rounding error or decimal parts.

But this is nitpicking a tongue-in-cheek comment.

permalink
report
parent
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

  • 3

    Monthly active users

  • 31

    Posts

  • 229

    Comments

Community moderators