Last active
August 29, 2015 14:09
-
-
Save simonguest/02cfc26fed8577d582c7 to your computer and use it in GitHub Desktop.
Locker Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var _ = require('underscore'); | |
var LOCKERS = 100; | |
// close all of the lockers | |
var lockers = _.range(1, LOCKERS + 1).map(function (i) { | |
return {number: i, open: false} | |
}); | |
// open the lockers by student | |
_.range(1, LOCKERS + 1).map(function (locker) { | |
_.range(1, LOCKERS + 1).map(function (student) { | |
if (student % locker != 0) { | |
lockers[student - 1].open = !lockers[student - 1].open; | |
} | |
}) | |
}); | |
// display the results | |
console.log(lockers); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A new high school has just been completed. There are 100 lockers in the school and they have been numbered from 1 through 100. During recess (remember this is a fictional problem), the students decide to try an experiment. When recess is over each student will walk into the school one at a time. The first student will open all of the locker doors. The second student will close all of the locker doors with even numbers. The third student will change all of the locker doors that are multiples of 3 (change means closing
lockers that are open, and opening lockers that are closed.) The fourth student will change the position of all locker doors numbered with multiples of four and so on. After 100 students have entered the school, which locker doors will be open, and why?