in reply to Proving Veritasiums riddle using Perl
Neither did I reveal the spoiler, nor did look at the Youtube post.
The arrangement of the prisoners' numbers in the boxes is a permutation of 100 numbers. Every permutation consists of a number of cycles. I believe that chances are not too high that any given permutation has a cycle that is larger than half of the number of its elements. When every prisoner follows the cycle starting with his own number, they will stay in prison only if there is a cycle larger than 50 in the given permutation.
I don't want to claim this strategy would be optimal, but it looks like chances of getting free are around 0.31 with this approach.
#!/usr/bin/perl use v5.16; use warnings; use List::Util 'shuffle'; use constant N => 100000; sub doit { my @box = shuffle 0 .. 99; prisoner: for my $prisoner (0 .. 99) { my $box = $prisoner; for (0 .. 49) { my $found = $box[$box]; next prisoner if $prisoner == $found; $box = $found; } return; } 1; } my $success; for (1..N) { $success++ if doit(); } say $success / N; __DATA__ 0.31257
Greetings,
🐻
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Proving Veritasiums riddle using Perl
by LanX (Saint) on May 04, 2025 at 17:14 UTC | |
by jo37 (Curate) on May 04, 2025 at 19:42 UTC |