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

Published on 12 May 2024 09:40 PM

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

Source code in the zkl programming language

const SLOTS=100, PRISONERS=100, TRIES=50, N=10_000;
fcn oneHundredJDI{	// just do it strategy
   cupboard,picks := [0..SLOTS-1].walk().shuffle(), cupboard.copy();
   // if this prisoner can't find their number in TRIES, all fail
   foreach p in (PRISONERS){ if(picks.shuffle().find(p)>=TRIES) return(False); }
   True		// all found their number
}
fcn oneHundredO{	// Optimal strategy
   cupboard := [0..SLOTS-1].walk().shuffle();
   foreach p in (PRISONERS){
      d:=p;
      do(TRIES){ if((d=cupboard[d]) == p) continue(2) }  // found my number
      return(False);  // this prisoner failed to find their number, all fail
   }
   True		// all found their number
}

s:=N.pump(Ref(0).incN,oneHundredJDI).value.toFloat()/N*100;
println("Just do it strategy (%,d simulatations): %.2f%%".fmt(N,s));

s:=N.pump(Ref(0).incN,oneHundredO).value.toFloat()/N*100;
println("Optimal strategy    (%,d simulatations): %.2f%%".fmt(N,s));

const SLOTS=100, PRISONERS=10, TRIES=50, N=100_000;

  

You may also check:How to resolve the algorithm GSTrans string conversion step by step in the BBC BASIC programming language
You may also check:How to resolve the algorithm Draw a rotating cube step by step in the PostScript programming language
You may also check:How to resolve the algorithm Sleep step by step in the Applesoft BASIC programming language
You may also check:How to resolve the algorithm Semiprime step by step in the Lambdatalk programming language
You may also check:How to resolve the algorithm Text processing/Max licenses in use step by step in the AutoHotkey programming language