<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)
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.