### re: Challenge: Write your worst program VIEW POST

This is not the worst program I've ever written, but it's pretty bad.

I recently participated in the ICPC (International Collegiate Programming Contest) at my school. To get everyone used to the judging system and the idea of a programming contest, it starts with a 45 minute long practice contest with one problem, which is "input a number and output the sum of the numbers from 1 to that number."

Of course, I could have written a program to do that in 5 minutes. But a guy on the team that eventually got first place said, "Theodore, I expect you to do this in the most insane and inefficient way possible." So I wrote this (as best as I can remember):

``````#include <stdio.h>
#define s(n) (((n) * ((n) + 1)) / 2)
#define s1(n) s(n),s(n+1)
#define s2(n) s(n),s(n+2)
#define s3(n) s(n),s(n+4)
#define s4(n) s(n),s(n+8)
#define s5(n) s(n),s(n+16)
#define s6(n) s(n),s(n+32)
#define s7(n) s(n),s(n+64)
#define s8(n) s(n),s(n+128)
#define s9(n) s(n),s(n+256)
#define s10(n) s(n),s(n+512)
#define s11(n) s(n),s(n+1024)
#define s12(n) s(n),s(n+2048)
#define s13(n) s(n),s(n+4096)
#define s14(n) s(n),s(n+16384)
#define s15(n) s(n),s(n+32768)
#define s16(n) s(n),s(n+65536)
#define s17(n) s(n),s(n+131072)
#define s18(n) s(n),s(n+262144)
#define s19(n) s(n),s(n+524288)
long lookup[] = {s19(0L)};

int main() {
char buf;
fgets(buf, sizeof(buf), stdin);
int n = atoi(buf);
int result = (n * (n + 1)) / 2;
if (n < sizeof(lookup)/sizeof(lookup))
result = lookup[n];
printf("%d\n", lookup);
}
``````

When you compile this, the C compiler will, after about 30 seconds, generate a lookup table with the answer for any input value up to 1,048,575. This isn't the absolute maximum number you could input though, so as a failsafe, it first calculates the answer at runtime, and then checks if the input is in the lookup table. If it is, then it uses the value from the lookup table instead.

When I came back from the practice contest, I told the other team about what I'd done, and they said "whoa that's the perfect way to do it." They'd written a Python program to generate a C program with the lookup table. They also showed me a screenshot from a judge's machine saying:

``````Compiling...
00:00:13
``````

haha nice!

code of conduct - report abuse  