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
  • 1
    \$\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:58
  • \$\begingroup\$ @Neil it depends on your definition of "direct". As an example "4Ḍ"£Ƙ®»Ṿ€;€)ḷ$)=¡ṭⱮṁØ%J’ż@Ɗ (28 bytes, but may be improbable - replace Ø% with 32 to try it out :p) gives no header or footer with sections like "<parity>"ḷ$=¡<i> e.g. "even"ḷ$=¡14, but prints \$n\$ if it's not \$i\$ and does not work in exactly the same way (each non-initial if statement is actually testing the output of the previous if statement, which is either \$n\$ or a parity string we've found). \$\endgroup\$ Commented Sep 25, 2024 at 12:11
  • \$\begingroup\$ *improbable -> improvable. (Same for 26 with "4Ḍ"£Ƙ®»Ṿ€"ḷ$=¡"ṭⱮṁ32J’ż@Ɗ) \$\endgroup\$ Commented Sep 25, 2024 at 12:48
  • \$\begingroup\$ Thanks, as long as the code only prints odd or even for those sections that have been included then that seems to be reasonable. \$\endgroup\$ Commented Sep 25, 2024 at 13:00
  • \$\begingroup\$ Are you sure this is valid? The crux of the challenge seems to be that the generated programme should make use of conditionals. \$\endgroup\$ Commented Sep 26, 2024 at 23:43

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