How to resolve the algorithm 100 prisoners step by step in the Phix programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 100 prisoners step by step in the Phix 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 Phix programming language
Source code in the phix programming language
function play(integer prisoners, iterations, bool optimal)
sequence drawers = shuffle(tagset(prisoners))
integer pardoned = 0
bool found = false
for i=1 to iterations do
drawers = shuffle(drawers)
for prisoner=1 to prisoners do
found = false
integer drawer = iff(optimal?prisoner:rand(prisoners))
for j=1 to prisoners/2 do
drawer = drawers[drawer]
if drawer==prisoner then found = true exit end if
if not optimal then drawer = rand(prisoners) end if
end for
if not found then exit end if
end for
pardoned += found
end for
return 100*pardoned/iterations
end function
constant iterations = 100_000
printf(1,"Simulation count: %d\n",iterations)
for prisoners in {10,100} do
atom random = play(prisoners,iterations,false),
optimal = play(prisoners,iterations,true)
printf(1,"Prisoners:%d, random:%g, optimal:%g\n",{prisoners,random,optimal})
end for
You may also check:How to resolve the algorithm Simple turtle graphics step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Ethiopian multiplication step by step in the Relation programming language
You may also check:How to resolve the algorithm Ludic numbers step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the TPP programming language
You may also check:How to resolve the algorithm Accumulator factory step by step in the Ring programming language