Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
51
Task/100-prisoners/Mathematica/100-prisoners.math
Normal file
51
Task/100-prisoners/Mathematica/100-prisoners.math
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
ClearAll[PlayRandom, PlayOptimal]
|
||||
PlayRandom[n_] :=
|
||||
Module[{pardoned = 0, sampler, indrawer, found, reveal},
|
||||
sampler = indrawer = Range[100];
|
||||
Do[
|
||||
indrawer //= RandomSample;
|
||||
found = 0;
|
||||
Do[
|
||||
reveal = RandomSample[sampler, 50];
|
||||
If[MemberQ[indrawer[[reveal]], p],
|
||||
found++;
|
||||
]
|
||||
,
|
||||
{p, 100}
|
||||
];
|
||||
If[found == 100, pardoned++];
|
||||
,
|
||||
{n}
|
||||
];
|
||||
N[pardoned/n]
|
||||
]
|
||||
PlayOptimal[n_] :=
|
||||
Module[{pardoned = 0, indrawer, reveal, found, card},
|
||||
indrawer = Range[100];
|
||||
Do[
|
||||
indrawer //= RandomSample;
|
||||
Do[
|
||||
reveal = p;
|
||||
found = False;
|
||||
Do[
|
||||
card = indrawer[[reveal]];
|
||||
If[card == p,
|
||||
found = True;
|
||||
Break[];
|
||||
];
|
||||
reveal = card;
|
||||
,
|
||||
{g, 50}
|
||||
];
|
||||
If[! found, Break[]];
|
||||
,
|
||||
{p, 100}
|
||||
];
|
||||
If[found, pardoned++];
|
||||
,
|
||||
{n}
|
||||
];
|
||||
N[pardoned/n]
|
||||
];
|
||||
PlayRandom[1000]
|
||||
PlayOptimal[10000]
|
||||
Loading…
Add table
Add a link
Reference in a new issue