News for hackers. Stuff that matters.
Re:4B If Statements by xnorswap on Friday December 12, 2025 @10:45AM (#46245193)
This is time efficient* but rather wasteful of space.

The best way to save space is to use a Bloom Filter.

If we capture all the even numbers, that would sadly only give us "Definitely not Even" or "Maybe Even".

But for just the cost of doubling our space, we can use two Bloom filters!

So we can construct one bloom filter capturing even numbers, and another bloom filter capturing odd numbers.

Now we have "Definitely not Even" and "Maybe Even" but also "Definitely not Odd" and "Maybe Odd".

In this manner, we can use the "evens" filter to find the odd numbers and the "odds" filter to find the even numbers.

Having done this, we'll be left with just a handful of unlucky numbers that are recorded as both "Maybe even" and "Maybe odd". These will surely be few enough in number that we can special case these in our if/else block.

The filters as a first-pass will save gigabytes of memory!

Re:4B If Statements by gopalv on Friday December 12, 2025 @11:22AM (#46245620)
> But for just the cost of doubling our space, we can use two Bloom filters!

We can optimize the hash function to make it more space efficient.

Instead of using remainders to locate filter positions, we can use a mersenne prime number mask (like say 31), but in this case I have a feeling the best hash function to use would be to mask with (2^1)-1.

Re:4B If Statements by AlotOfReading on Friday December 12, 2025 @12:47PM (#46246570)
This produced strange results on my ternary computer. I had to use a recursive popcnt instead.
Re:4B If Statements by piersadrian on Friday December 12, 2025 @02:49PM (#46248000)
this is my new favorite comment on this cursed website
Re:4B If Statements by nixpulvis on Friday December 12, 2025 @11:10AM (#46245477)
How is this time efficient at all? It takes upwards of 40 seconds to compute on large 32bit values.

It's a joke post with some interesting bits and details.

Re:4B If Statements by xnorswap on Friday December 12, 2025 @11:18AM (#46245573)
It's a constant number of lookups, and all good Computer Scientists know that it is therefore an O(1) algorithm.

It is hard to imagine better efficiency than O(1)!

Indeed we could improve it further by performing all evaluations even when we find the answer earlier, ensuring it is a true Constant Time algorithm, safe for use in cryptography.

Re:4B If Statements by nixpulvis on Friday December 12, 2025 @11:29AM (#46245693)
> This is time efficient* but rather wasteful of space.

You're saying that the blog's solution is time efficient. Which it is not. Your solution may be O(1) but it is also not efficient. As I'm sure you are aware.

I can tell you a practical solution which is also O(1) and takes up maybe 2 or 3 instructions of program code and no extra memory at all.

`x & 1` or `x % 2 != 0`

This blog post was taking a joke and running with it. And your comment is in that spirit as well, I just wanted to point out that it's by no means time efficient when we have 2s or 1s complement numbers which make this algorithm trivial.

Re:4B If Statements by icambron on Friday December 12, 2025 @11:38AM (#46245779)
You need to read their entire comment as a joke.
Re:4B If Statements by nixpulvis on Friday December 12, 2025 @11:48AM (#46245894)
I guess I should have been more clear that I was just pointing out the obvious in case some confused reader missed the joke.

lol

Re:4B If Statements by kbelder on Friday December 12, 2025 @12:14PM (#46246160)
Which was also obvious, but maybe also needed pointing out, which says something about online discussion. Something obvious, probably.
Re:4B If Statements by ricardo81 on Friday December 12, 2025 @12:12PM (#46246137)
explaining the joke spoils the joke, such is social convention.
Re:4B If Statements by nixpulvis on Friday December 12, 2025 @01:55PM (#46247377)
Forgive me for not being funny.
Re:4B If Statements by ricardo81 on Friday December 12, 2025 @10:37PM (#46251760)
It's alright. I don't make the rules.
Re:4B If Statements by henrikschroder on Friday December 12, 2025 @05:39PM (#46249890)
> I just wanted to point out that

We already know. Everybody knows. That's the joke. There's no need to point out anything.

Re:4B If Statements by Maxatar on Friday December 12, 2025 @11:51AM (#46245917)
The comment you're replying to is also a joke, with some interesting bits and details.
Re:4B If Statements by nixpulvis on Friday December 12, 2025 @01:56PM (#46247384)
I think I'll just avoid commenting on jokes from now on.
Re:4B If Statements by Sohcahtoa82 on Friday December 12, 2025 @12:28PM (#46246347)
How are you able to recognize a joke post but not a joke comment?
Re:4B If Statements by nixpulvis on Friday December 12, 2025 @01:54PM (#46247364)
I may have missed the * meaning. I got that the bloom filter was an extension of the joke as I mentioned below. I was just clarifying in case someone else missed the joke.
Re:4B If Statements by uplifter on Friday December 12, 2025 @01:22PM (#46246990)
You're absolutely right. The obvious solution would have been to create a boolean table containing all the pre-computed answers, and then simply use the integer you are testing as the index of the correct answer in memory. Now your isEven code is just a simple array lookup! Such an obvious improvement, I can't believe the OP didn't see it.

And with a little extra work you can shrink the whole table's size in memory by a factor of eight, but I'll leave that as an exercise for the interested reader.

Re:4B If Statements by mandarax8 on Friday December 12, 2025 @03:57PM (#46248864)
Maybe we can even find some correlation in the bit pattern of the input and the Boolean table!
Re:4B If Statements by __david__ on Sunday December 14, 2025 @08:17AM (#46262808)
Perhaps, but I fear you’re veering way too much into “clever” territory. Remember, this code has to be understandable to the junior members of the team! If you’re not careful you’ll end up with arcane operators, strange magic numbers, and a general unreadable mess.
Re:4B If Statements by jiggawatts on Friday December 12, 2025 @04:27PM (#46249183)
If the "exercise" is to strictly rely on if-else statements, then the obvious speedup is to perform a binary search instead of a linear one. The result would still be horrifically space inefficient, but the speed would be roughly the time it takes to load 32x 4KB pages randomly from disk (the article memory-mapped the file). On a modern SSD a random read is 20 microseconds, so that's less than a millisecond for an even/odd check!

"That's good enough, ship it to production. We'll optimise it later."

Re:4B If Statements by nrhrjrjrjtntbt on Friday December 12, 2025 @04:41PM (#46249331)
r/whoosh
Re:4B If Statements by alexjurkiewicz on Friday December 12, 2025 @08:56PM (#46251252)
You are absolutely write! Let me write a Go program to implement this idea. The bloom filters will take approximately 5gb (for a 1% error rate) and take a few minutes to populate on a modern Macbook Pro.

https://gist.github.com/alexjurkiewicz/1abf05f16fd98aabf380c...