How to resolve the algorithm 100 prisoners step by step in the XPL0 programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 100 prisoners step by step in the XPL0 programming language
Table of Contents
Problem Statement
Show and compare the computed probabilities of success for the two strategies, here, on this page.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 100 prisoners step by step in the XPL0 programming language
Source code in the xpl0 programming language
int Drawer(100);
proc KShuffle; \Randomly rearrange the cards in the drawers
\(Woe unto thee if Stattolo shuffle is used instead of Knuth shuffle.)
int I, J, T;
[for I:= 100-1 downto 1 do
[J:= Ran(I+1); \range [0..I]
T:= Drawer(I); Drawer(I):= Drawer(J); Drawer(J):= T;
];
];
func Stategy2; \Return 'true' if stragegy succeeds
int Prisoner, Card, Try;
[for Prisoner:= 1 to 100 do
[Card:= Drawer(Prisoner-1);
Try:= 1;
loop [if Card = Prisoner then quit;
if Try >= 50 then return false;
Card:= Drawer(Card-1);
Try:= Try+1;
];
];
return true;
];
func Stategy1; \Return 'true' if stragegy succeeds
int Prisoner, I, D(100);
[for Prisoner:= 1 to 100 do
loop [for I:= 0 to 100-1 do D(I):= I+1;
KShuffle;
for I:= 1 to 50 do
if Drawer(D(I-1)) = Prisoner then quit;
return false;
];
return true;
];
proc Strategy(S);
int S, I, Sample;
real Successes;
[Successes:= 0.;
for Sample:= 1 to 100_000 do
[for I:= 0 to 100-1 do Drawer(I):= I+1;
KShuffle;
case S of
1: if Stategy1 then Successes:= Successes + 1.;
2: if Stategy2 then Successes:= Successes + 1.
other [];
];
RlOut(0, Successes/100_000.*100.); Text(0, "%^m^j");
];
[Format(3, 12);
Text(0, "Random strategy success rate: ");
Strategy(1);
Text(0, "Optimal strategy success rate: ");
Strategy(2);
]
You may also check:How to resolve the algorithm Caesar cipher step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Egyptian division step by step in the Ring programming language
You may also check:How to resolve the algorithm Filter step by step in the Gambas programming language
You may also check:How to resolve the algorithm Quine step by step in the TXR programming language
You may also check:How to resolve the algorithm Video display modes step by step in the FreeBASIC programming language