Skip to content

Instantly share code, notes, and snippets.

@simonguest
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonguest/02cfc26fed8577d582c7 to your computer and use it in GitHub Desktop.
Save simonguest/02cfc26fed8577d582c7 to your computer and use it in GitHub Desktop.
Locker Challenge
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);
@simonguest
Copy link
Author

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment