Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

161–170 of 183 posts

Re: 4 billion if statements (2023)

#162

Earlier quoted context omitted.

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.

> 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…

> 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: 4 billion if statements (2023)

#165

I know it's silly, but I just want to fix his first version with the minimum possible changes; /* Copyright 2023. All unauthorized distribution of this source code will be persecuted to the fullest extent of the law*/ #include #include #include int main(int argc, char* argv[]) { uint8_t number = argc>1 ? argv[1][strlen(argv[1])-1]-'0' : printf("Usage: odd-or-even number\n"); if (number == 0) printf("even\n"); if (num…

Wouldn't using elif for all comparisons after the first improve performance?

Or is the performance considered worse because it becomes O(n) (where n < MAX_UINT) vs. constant time ( O(MAX_UINT) )

Re: 4 billion if statements (2023)

#167

I know it's silly, but I just want to fix his first version with the minimum possible changes; /* Copyright 2023. All unauthorized distribution of this source code will be persecuted to the fullest extent of the law*/ #include #include #include int main(int argc, char* argv[]) { uint8_t number = argc>1 ? argv[1][strlen(argv[1])-1]-'0' : printf("Usage: odd-or-even number\n"); if (number == 0) printf("even\n"); if (num…

Wouldn't using elif for all comparisons after the first improve performance? Or is the performance considered worse because it becomes O(n) (where n < MAX_UINT) vs. constant time ( O(MAX_UINT) )

It certainly would be normal to use else if (or switch) if you wanted to be picky but really such changes are inconsequential here. And I was trying to change just one line. Sadly I also had to quietly change stdlib.h to string.h as well.

Re: 4 billion if statements (2023)

#168

God help us if that code ever makes it's way onto npm. isEven is a performant, hand-compiled evenness checker for any 32 bit integer. A single file import that does one job and one job only!

it follows the UNIX philosophy of doing one thing and donig it well.

Donig!! LOL maybe a mistake maybe not but still funny

Re: 4 billion if statements (2023)

#169

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…

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...

Re: 4 billion if statements (2023)

#170

Earlier quoted context omitted.

Yeeessss! Microservices!

Your mention of Microservices opened up my mind to additional possibilities. How about we create a microservice for each integer, then deploy 4 billion of them. Send a request to all of them simultaneously. Only one of them will respond with the answer. We still need to decide how to deploy those microservices - one per machine, or multiple per machine?

Could try 1 per IPv4 address? There’s just barely enough!
Post reply on HN