How to resolve the algorithm 100 prisoners step by step in the J programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 100 prisoners step by step in the J 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 J programming language
Source code in the j programming language
NB. game is solvable by optimal strategy when the length (#) of the
NB. longest (>./) cycle (C.) is at most 50.
opt=: 50 >: [: >./ [: > [: #&.> C.
NB. for each prisoner randomly open 50 boxes ((50?100){y) and see if
NB. the right card is there (p&e.). if not return 0.
rand=: monad define
for_p. i.100 do. if. -.p e.(50?100){y do. 0 return. end.
end. 1
)
NB. use both strategies on the same shuffles y times.
simulate=: monad define
'o r'=. y %~ 100 * +/ ((rand,opt)@?~)"0 y # 100
('strategy';'win rate'),('random';(":o),'%'),:'optimal';(":r),'%'
)
You may also check:How to resolve the algorithm Long multiplication step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Magic 8-ball step by step in the Transd programming language
You may also check:How to resolve the algorithm ABC problem step by step in the Julia programming language
You may also check:How to resolve the algorithm Determine if only one instance is running step by step in the Ring programming language
You may also check:How to resolve the algorithm Soundex step by step in the Ring programming language