Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Print 4 billion if statements

Recently (okay, December 2023, I'm a little late) there's been a meme going around about a program checking if a 32-bit unsigned integer is even or odd using four billion if statements:

if num == 0:
 print("even")
if num == 1:
 print("odd")
if num == 2:
 print("even")
...
if num == 4294967294:
 print("even")
if num == 4294967295:
 print("odd")

Today, you'll be writing a program/function that outputs something similar to this. Your code should take no input and output source code, in the same language, for a function or program that takes an unsigned 32-bit integer (0 ≤ n ≤ 4294967295) and outputs/returns either "even" if the input is even and "odd" otherwise. For clarity, I'll use "program" for the rest of this post, but everything here also applies to function submissions.

Specifically, the resulting program should be divisible into a "header", 4294967296 "segments", and a "footer", such that concatenating the header, the i+1th segment, and the footer results in a program that:

  • When given i, prints either "even" or "odd" depending on i's parity
  • When given any unsigned 32-bit integer that's not i, does something else - this can include crashing, printing nothing, or printing anything else, as long as it doesn't print only "odd" or "even".

For example, with the following C code:

#include <stdlib.h>
int main(int argc, char* argv[]) {
 unsigned int number = atoi(argv[1]);
 if (number == 0)
 printf("even\n");
 if (number == 1)
 printf("odd\n");
 if (number == 2)
 printf("even\n");
 // etc etc
 if (number == 4294967294)
 printf("even\n");
 if (number == 4294967295)
 printf("odd\n");
}

Here, the #include <stdlib.h>; int main(int argc, char* argv[]) { unsigned int number = atoi(argv[1]); is the header, the } is the footer, and the if (number == 2) printf("even\n"); are the segments. Concatenating e.g. the 136th segment with the header and footer results in the following:

#include <stdlib.h>
int main(int argc, char* argv[]) {
 unsigned int number = atoi(argv[1]);
 if (number == 135)
 printf("odd\n");
}

which indeed prints "odd" if given 135 and outputs nothing otherwise.

Here's an example of a generating program in Python.

As per standard I/O, "even" / "odd" may be output with any (including none) amount of leading/trailing whitespace. It's fine if, due to program size/memory/language limitations (e.g. Java source files being at most 1GB), the resulting program doesn't compile, as long as it works in theory.

This is , the shortest generating program wins!

Answer*

Draft saved
Draft discarded
Cancel
6
  • \$\begingroup\$ Out of interest, how long would a generator for a direct port of the meme be? (I'll accept a port that uses if/else rather than separate if statements.) \$\endgroup\$ Commented Sep 25, 2024 at 5:57
  • \$\begingroup\$ Japt doesn't have if(/else) statements, @Neil. The closest I could get to the spec would be to use logical AND with explicit output, which would be the same byte count. \$\endgroup\$ Commented Sep 25, 2024 at 10:57
  • \$\begingroup\$ So why does your description say If 0 is equal to U? (It was really the specific odd/even output per if statement that I was looking for anyway.) \$\endgroup\$ Commented Sep 25, 2024 at 11:15
  • \$\begingroup\$ That was just me combining the explanation of the (stritcty equal to comparison operator) and the ternary operator into one line. Alternating between "even" and "odd" for each statement would cost me ~4 bytes. \$\endgroup\$ Commented Sep 25, 2024 at 11:22
  • 1
    \$\begingroup\$ Fair enough, the ternary operator also counts as if/else for me, thanks. \$\endgroup\$ Commented Sep 25, 2024 at 12:56

AltStyle によって変換されたページ (->オリジナル) /