Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

61–70 of 183 posts

Re: 4 billion if statements (2023)

#61

I have never seen anyone argue for a ‘switch’ version. switch (v) { case: 0,2,4,8,…: return EVEN; case: 1,3,5,7,…: return ODD; default: return IDK; } Slightly less code to generate.

you forgot the logic to strip the final digit and assign it to v. processing the whole number is absurd

Converting to decimal is just as absurd.

All you need is the final binary digit, which incidentally is the most optimal codegen, `v & 1`.

Re: 4 billion if statements (2023)

#64
post #8

Gemini took 4 seconds to answer this prompt: "Here is a number 4200020010101. Think deeply about it and tell me if it is not or or not even." So if you're concerned with privacy issues, you can run the assembly version proposed in the article locally and be well within the same order of performance. Let's thank the author of the article for providing a decent alternative to Google. ah, but the license is not that goo…

Finally a problem that Microsoft Phi can ace. Probably. Maybe. Some of the time at least.

Re: 4 billion if statements (2023)

#66
This could be obviously done with much less code: Just add "if"s for all even number, and at the end just return "odd" if none of the evens matched. 50% less code!

Or even simpler: If it's 0, return "even". If not, do a recursive call to n-1, if that equals "even", return "odd", otherwise return "even".

But the best way is probably to just use a library. Yes, 500MB of additional dependencies, but then it's a one-liner.

Re: 4 billion if statements (2023)

#68
post #6

if(n&1) else

You can do it even faster with the if statements: #include #include int main(int argc, char *argv[]) { if (argc \n", argv[0]); return 1; } char *s = argv[1]; int i; /* find the end of the string */ for (i = 0; s[i] != '\0'; ++i) ; /* make sure the string wasn't empty */ if (i == 0) { fprintf(stderr, "Error: empty string\n"); return 1; } /* last character is at s[i - 1] */ char d = s[i - 1]; if (d == '0') printf("even…

You inspired me this joyful rewrite:

    #define _(e) { e;};
    #define r(e) _(return e)
    #define I(b, e) _(if (b) r(e));
    #define W(e) _(while (1) _(e));
    int main(int c, char **v) {
      _(I(c != 2, -1) _(c = 0) W(I(!v[1][c++], v[1][c - 2] & 1)))
    }

Re: 4 billion if statements (2023)

#70

This could be obviously done with much less code: Just add "if"s for all even number, and at the end just return "odd" if none of the evens matched. 50% less code! Or even simpler: If it's 0, return "even". If not, do a recursive call to n-1, if that equals "even", return "odd", otherwise return "even". But the best way is probably to just use a library. Yes, 500MB of additional dependencies, but then it's a one-line…

You could save stack space by transforming it into a loop. It’s still only O(n)!
Post reply on HN