JSFiddle - React, Tailwind, and code Playground

by Andrew Maxwell

HTML

<body ng-app="app" ng-controller="ctrl">
    
    <p>{{ num }} people numbered 1 - {{ num }} have their number put into a random, unoccupied box. The boxes are also numbered 1 - {{ num }} and have their number on the outside. One at a time, each person is allowed to look into half of the boxes in hopes of finding their number. Before anyone checks the boxes, they are allowed to form a strategy. After this, though, there is no communication and nothing about the boxes or numbers can be changed in any way. How can they maximize their chances of each person finding their number?</p>

CSS

* {font-family: sans-serif}

JavaScript

var app = angular.module("app", [])

app.controller("ctrl", function($scope){
  
    $scope.num = 100
    
    $scope.calc
    
})


function shuffle(o){ //v1.0
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

function calc(num) {
   var boxes = new Array(num)
   var found = 0
   
   for (var i = 0; i < num; i++){
       boxes[i] = i
   }
    
   shuffle(boxes)
   
   for (var i = 0; i < num; i++){
       var curr = boxes[i]
       for (var j = 0; j < num / 2 && curr != i; j++){
           curr = boxes[curr]
       }
       if (curr == i) found++
   }
   
   return found
   
}

var n = 10000
var numPeople = 100
for (var i = 0; i < n; i++){
    var found = calc(numPeople)
    if (found == numPeople)