How to resolve the algorithm 100 prisoners step by step in the 11l programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 100 prisoners step by step in the 11l 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 11l programming language

Source code in the 11l programming language

F play_random(n)
   V pardoned = 0
   V in_drawer = Array(0.<100)
   V sampler = Array(0.<100)
   L 0 .< n
      random:shuffle(&in_drawer)
      V found = 0B
      L(prisoner) 100
         found = 0B
         L(reveal) random:sample(sampler, 50)
            V card = in_drawer[reveal]
            I card == prisoner
               found = 1B
               L.break
         I !found
            L.break
      I found
         pardoned++
   R Float(pardoned) / n * 100

F play_optimal(n)
   V pardoned = 0
   V in_drawer = Array(0.<100)
   L 0 .< n
      random:shuffle(&in_drawer)
      V found = 0B
      L(prisoner) 100
         V reveal = prisoner
         found = 0B
         L 50
            V card = in_drawer[reveal]
            I card == prisoner
               found = 1B
               L.break
            reveal = card
         I !found
            L.break
      I found
         pardoned++
   R Float(pardoned) / n * 100

V n = 100'000
print(‘ Simulation count: ’n)
print(‘ Random play wins: #2.1% of simulations’.format(play_random(n)))
print(‘Optimal play wins: #2.1% of simulations’.format(play_optimal(n)))

  

You may also check:How to resolve the algorithm Write entire file step by step in the REXX programming language
You may also check:How to resolve the algorithm Echo server step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Barnsley fern step by step in the VBA programming language
You may also check:How to resolve the algorithm Four bit adder step by step in the MATLAB / Octave programming language
You may also check:How to resolve the algorithm Non-decimal radices/Convert step by step in the M2000 Interpreter programming language