March 2014 update

This commit is contained in:
Ingy döt Net 2014-04-02 16:56:35 +00:00
parent 09687c4926
commit a25938f123
1846 changed files with 21876 additions and 5203 deletions

View file

@ -1,19 +1,9 @@
var
n = 100,
doors = [n],
step,
idx;
// now, start opening and closing
for (step = 1; step <= n; step += 1)
for (idx = step; idx <= n; idx += step)
// toggle state of door
doors[idx] = !doors[idx];
Array.apply(null, { length: 100 })
.map(function(v, i) { return i + 1; })
.forEach(function(door) {
var sqrt = Math.sqrt(door);
// find out which doors are open
var open = doors.reduce(function(open, val, idx) {
if (val) {
open.push(idx);
}
return open;
}, []);
document.write("These doors are open: " + open.join(', '));
if (sqrt === (sqrt | 0)) {
console.log("Door %d is open", door);
}
});