Create a program that halts exactly 50% of the time. Be original. Highest voted answer wins. By exactly I mean that on each run there is a 50% chance of it halting.
-
9\$\begingroup\$ I mean that it should have an exactly 50% probability to halt on every run. \$\endgroup\$– ikeJan 1, 2014 at 12:41
-
5\$\begingroup\$ If the program doesn't halt, does that mean it runs forever? It'll sure as hell halt when I turn the PC off. (Unless it is NSA code, then who knows...) \$\endgroup\$– PaulJan 1, 2014 at 12:44
-
8\$\begingroup\$ Who keeps upvoting these poor questions? \$\endgroup\$– GarethJan 2, 2014 at 11:04
-
6\$\begingroup\$ This is a fine question. Only those who don't understand probability are confused by it. The original title was perhaps a bit misleading, but no worse than the New York Times. \$\endgroup\$– Keith RandallJan 3, 2014 at 4:45
-
4\$\begingroup\$ I found it perfectly clear. Create a program that has a 50% chance of halting (or, equivalently a 50% chance of falling into an infinite loop), and you cannot know which will occur before every runtime. \$\endgroup\$– ejrbJan 3, 2014 at 9:25
42 Answers
Exactly 50% of the time?
OBJ-C
- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
BOOL haltedLastRun = [(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"halted"] boolValue];
if (!haltedLastRun) {
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"halted"];
[[NSApplication sharedApplication] terminate:nil];
}
}
Haskell
Runs for two intervals, each one 1 second long (chosen because 1 second is the SI unit for time). Halts inside 50% of the intervals. So 50% of the running seconds it will not halt, the other 50% it will. Works in GHC only.
import Control.Concurrent (threadDelay)
main = threadDelay 1990000
Shell Script
this script will clobber .md5sum files in the current and child directories.
#!/bin/sh
echo *.md5sum|xargs -n1|head -n1|xargs test -e && exec rm *.md5sum
while ! find . -name '*.md5sum' -print0 |xargs -0r grep 00000000000000
do {
find . -type f -print|sed -e 's!^\(.*\)$!md5sum "\1" > "\1".md5sum!e'
}
done
C++
#include <fstream>
main () {
int c;
std::fstream fs;
fs.open ("myfile.txt", std::fstream::in);
fs>>c;
fs.close ();
fs.open ("myfile.txt", std::fstream::out);
fs<<c+1;
fs.close ();
while (c%2);
return 0;
}
Each run will halt iff the run before didn't.
Windows Command Script
This script will append code to itself which ultimately alternates 'x' on each run.
call :last
if %x%==1 (
echo>>%0 set x=0
exit /b 0
) else (
echo>>%0 set x=1
)
:nohalt
goto :nohalt
:last
set x=1
[newline here]
Python 2, 54 bytes
import time;H=[time.time()%2]
for h in H:H+=[h]*int(h)
Halting behaviour is dependent on current time.
Pxem, filename: 7 bytes + content: 0 bytes = 7 bytes, depends on implementation.
- Filename (escaped):
\002.r.w.a
- Content: empty.
How it works
\002.r
: pushes one of 0 or 1..w ... .a
:while empty || pop!=0; do ... ;done
Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes
2RZw?[
Explanation
2R # range(2): push [0, 1]
Zw # Random element of ↑
? # If ↑ == 1:
[ # Loop forever
# (Else, exit)
C (GCC)
unsigned long long i,j,k;
r;
main()
{
srand(time(0));
i=k=1;
while (k) {
k = 0;
for (j = 1; j <= i; ++j) {
do {
r = rand() % 4;
} while (r == 3);
k += !!r;
}
++i;
}
}
It loops while k
isn't 0. k
is only 0 at the end of the loop if i
number of calls to rand()
are all divisible by 3. The chance of that equals
$$ \frac {1} {3^i} $$
and the sum of all inverse natural powers of 3 equals 0.5. Of course, i
doesn't go from 1 to infinity, but from 1 to ULLONG_MAX
. When i
reaches ULLONG_MAX
, the for
loop will loop infinitely as j
will always be less than or equal to i
(assuming ULL_MAX + 1
is 0).
Since i
doesn't quite loop from 1 to infinity, the code will loop slightly less than 50 % of the time, more specifically
$$ 50 \% - \frac {1} {3^{U+1} - 3^{U}} $$
where U is ULLONG_MAX
.