Jump to content

100 prisoners: Difference between revisions

1-based index
(1-based index)
Line 2,007:
See [[#Pascal]].
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">for i range= 1 to 100
drawer[] &= i
sampler[] &= i
Line 2,014:
for i = len drawer[] downto 2
r = random i
swap drawer[r] drawer[i - 1]
.
.
subr play_random
call shuffle_drawer
foundfor prisoner = 1 to 100
prisoner = 0
while prisoner < 100 and found = 1
found = 0
for i = 01 to 50
while i < 50 and found = 0
r = random (100 - i)
card = drawer[sampler[r]]
Line 2,030 ⟶ 2,027:
if card = prisoner
found = 1
i += break 1
.
i += 1
.
prisonerif found += 10
i +=break 1
.
.
.
subr play_optimal
call shuffle_drawer
foundfor prisoner = 1 to 100
prisoner = 0
while prisoner < 100 and found = 1
reveal = prisoner
found = 0
for i = 01 to 50
while i < 50 and found = 0
card = drawer[reveal]
if card = prisoner
found = 1
break 1
.
reveal = card
i += 1
.
prisonerif found += 10
break 1
.
.
.
n = 10000
pardonedwin = 0
for round_ range= 1 to n
call play_random
pardonedwin += found
.
print "random: " & 100.0 * pardonedwin / n & "%"
#
pardonedwin = 0
for round_ range= 1 to n
call play_optimal
pardonedwin += found
.
print "optimal: " & 100.0 * pardonedwin / n & "%"</syntaxhighlight>
{{out}}
<pre>
2,060

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.