The never-ending finite loop
daemonology.net
daemonology.net
This is just trying to write a short program that'll run for a long time. Meh.
for(int x[99]={1};!x[98];++*x)*x*=!++x[*x]; for(int x[99]={1};++x[++*x]?*x=1:!x[98];); for(int x[99]={98};++x[*x]?*x=98:--*x;); for(int x[99]={1};++x[*x]?*x=98:--*x;);If you want an analogy: sorting an array is clearly a task that can be solved in O(n log n) time, but if you write a bubble sort implementation (or any other O(n^2) algorithm), the program will still take O(n^2) time.
You could write a program that runs in polynomial time, but be unable to prove that it does. Then, later, you could find a proof that the program runs in polynomial time. In fact, if P=NP, you can write such a program right now:
for each string s in the enumeration of all strings {
if (s = p + q where
(p is a program and
q is a proof that p solves 3SAT in polynomial time)) {
run p on the input;
return;
}
}
Also, you could interleave the execution of this program with one that solves 3SAT in exponential time. Then you'll have a program that works in any case, and that solves 3SAT in polynomial time if P = NP. char i,x[99];for(x[98]=i=1;x[98];i++)i*=!++x[i];
Here is my improved version int i,x[99];for(x[98]=i=1;x[98];)i*=!++x[i++];The possibility that I consider unlikely, of course, is that I've broken some assumption that causes the compiler to optimize cause an entirely undefined third behavior (such as crashing). Then the outcome is undefined. That is the kind of implementation detail that code golf often brushes against. I'm not sure whether a solution that includes that possibility but which happens to work on, say, gcc is considered legal.
But that suggests the following unambiguous form:
int i,x[99];for(x[98]=i=1;x[98];)i*=!++x[++i];It's undefined behavior. Any outcome is likely. The compiler could halt your program immediately if it wanted to, and that's just as likely as either of the cases you offer.
int i,x[99];for(x[98]=i=1;x+98;)i*=!++x[i++];
The other array references are l-values.
His first example of "for (int i = 1; i > 0; i++);" Would be matched by some ugly trick like:
int i = 0; while (!i);
That's warranted to halt as the chances for a bit in i to be flipped by cosmic rays tends to be 1 over time.
You are of course welcome to find this uninteresting, but "I can think of lots of big complicated programs that would terminate but not do so for a very long time" and "In the real world, an infinite loop may terminate because of a hardware malfunction" don't seem to me like good reasons for finding it uninteresting.