12/12/2025 at 3:45:09 PM
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!
by xnorswap
12/12/2025 at 4:22:57 PM
> 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.
by gopalv
12/12/2025 at 5:47:56 PM
This produced strange results on my ternary computer. I had to use a recursive popcnt instead.by AlotOfReading
12/12/2025 at 7:49:01 PM
this is my new favorite comment on this cursed websiteby piersadrian
12/13/2025 at 1:56:21 AM
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...
by alexjurkiewicz
12/12/2025 at 4:10:36 PM
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.
by nixpulvis
12/12/2025 at 4:18:56 PM
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.
by xnorswap
12/12/2025 at 4:29:30 PM
> 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.
by nixpulvis
12/12/2025 at 4:38:36 PM
You need to read their entire comment as a joke.by icambron
12/12/2025 at 4:48:38 PM
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
by nixpulvis
12/12/2025 at 5:14:30 PM
Which was also obvious, but maybe also needed pointing out, which says something about online discussion. Something obvious, probably.by kbelder
12/12/2025 at 5:12:35 PM
explaining the joke spoils the joke, such is social convention.by ricardo81
12/12/2025 at 6:55:37 PM
Forgive me for not being funny.by nixpulvis
12/13/2025 at 3:37:57 AM
It's alright. I don't make the rules.by ricardo81
12/12/2025 at 10:39:14 PM
> I just wanted to point out thatWe already know. Everybody knows. That's the joke. There's no need to point out anything.
by henrikschroder
12/12/2025 at 5:28:32 PM
How are you able to recognize a joke post but not a joke comment?by Sohcahtoa82
12/12/2025 at 6:54:23 PM
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.by nixpulvis
12/12/2025 at 6:22:42 PM
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.
by uplifter
12/12/2025 at 9:27:28 PM
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."
by jiggawatts
12/12/2025 at 8:57:57 PM
Maybe we can even find some correlation in the bit pattern of the input and the Boolean table!by mandarax8
12/14/2025 at 1:17:24 PM
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.by __david__
12/12/2025 at 4:51:42 PM
The comment you're replying to is also a joke, with some interesting bits and details.by Maxatar
12/12/2025 at 6:56:13 PM
I think I'll just avoid commenting on jokes from now on.by nixpulvis
12/12/2025 at 9:41:35 PM
r/whooshby nrhrjrjrjtntbt