100 doors: Difference between revisions

Content added Content deleted
(Output not needed or useful as is not really ran to generate page. Fixed whitespace.)
Line 6,544: Line 6,544:
=== ES5 ===
=== ES5 ===
==== Iterative ====
==== Iterative ====
<lang javascript>
<lang javascript>var doors=[];
var doors=[];
for (var i=0;i<100;i++)
doors[i]=false;
for(var i=0;i<100;i++)
for (var i=1;i<=100;i++)
doors[i]=false; //create doors
for(var i=1;i<=100;i++)
for (var i2=i-1,g;i2<100;i2+=i)
doors[i2]=!doors[i2];
for(var i2=i-1,g;i2<100;i2+=i)
for (var i=1;i<=100;i++)
doors[i2]=!doors[i2]; //toggle doors
console.log("Door %d is %s",i,doors[i-1]?"open":"closed")</lang>
for(var i=1;i<=100;i++) //read doors
console.log("Door %d is %s",i,doors[i-1]?"open":"closed")
</lang>

====Functional Composition====
====Functional Composition====

Naive search
Naive search
<lang JavaScript>(function (n) {
<lang JavaScript>(function (n) {
'use strict';
"use strict";


// finalDoors :: Int -> [(Int, Bool)]
function finalDoors(n) {
function finalDoors(n) {
var lstRange = range(1, n);
var lstRange = range(1, n);

return lstRange
return lstRange
.reduce(function (a, _, k) {
.reduce(function (a, _, k) {
var m = k + 1;
var m = k + 1;

return a.map(function (x, i) {
return a.map(function (x, i) {
var j = i + 1;
var j = i + 1;

return [j, j % m ? x[1] : !x[1]];
return [j, j % m ? x[1] : !x[1]];
});
});
Line 6,580: Line 6,570:
));
));
};
};



// GENERIC FUNCTIONS

// zip :: [a] -> [b] -> [(a,b)]
function zip(xs, ys) {
function zip(xs, ys) {
return xs.length === ys.length ? (
return xs.length === ys.length ? (
Line 6,593: Line 6,577:
) : undefined;
) : undefined;
}
}

// replicate :: Int -> a -> [a]
function replicate(n, a) {
function replicate(n, a) {
var v = [a],
var v = [a],
o = [];
o = [];

if (n < 1) return o;
if (n < 1) return o;
while (n > 1) {
while (n > 1) {
Line 6,607: Line 6,588:
return o.concat(v);
return o.concat(v);
}
}

// range(intFrom, intTo, optional intStep)
// Int -> Int -> Maybe Int -> [Int]
function range(m, n, delta) {
function range(m, n, delta) {
var d = delta || 1,
var d = delta || 1,
Line 6,616: Line 6,594:
a = Array(lng),
a = Array(lng),
i = lng;
i = lng;

if (blnUp)
if (blnUp)
while (i--) a[i] = (d * i) + m;
while (i--) a[i] = (d * i) + m;
else
else
while (i--) a[i] = m - (d * i);
while (i--) a[i] = m - (d * i);

return a;
return a;
}
}


return finalDoors(n)
return finalDoors(n)
.filter(function (tuple) {
.filter(function (tuple) {
Line 6,638: Line 6,612:


})(100);</lang>
})(100);</lang>
==== Optimized (iterative)====

{{out}}
<lang JavaScript>[{"door":1, "open":true}, {"door":4, "open":true}, {"door":9, "open":true}, {"door":16, "open":true}, {"door":25, "open":true}, {"door":36, "open":true}, {"door":49, "open":true}, {"door":64, "open":true}, {"door":81, "open":true}, {"door":100, "open":true}]</lang>

==== Optimized ( iterative )====
<lang javascript>for (var door = 1; door <= 100; door++) {
<lang javascript>for (var door = 1; door <= 100; door++) {
var sqrt = Math.sqrt(door);
var sqrt = Math.sqrt(door);
Line 6,649: Line 6,619:
}
}
}</lang>
}</lang>


Simple for loop. Optimizing the optimized?
Simple for loop. Optimizing the optimized?
<lang javascript>for(var door=1;i<10/*Math.sqrt(100)*/;i++){
<lang javascript>for(var door=1;i<10/*Math.sqrt(100)*/;i++){
console.log("Door %d is open",i*i);
console.log("Door %d is open",i*i);
}</lang>
}</lang>
==== Optimized (functional) ====

==== Optimized ( functional ) ====

The question of which doors are flipped an odd number of times reduces to the question of which numbers have an odd number of integer factors.
The question of which doors are flipped an odd number of times reduces to the question of which numbers have an odd number of integer factors.

We can simply search for these:
We can simply search for these:

<lang JavaScript>(function (n) {
<lang JavaScript>(function (n) {
'use strict';
"use strict";

return range(1, 100)
return range(1, 100)
.filter(function (x) {
.filter(function (x) {
Line 6,670: Line 6,633:
.length % 2;
.length % 2;
});
});

function integerFactors(n) {
function integerFactors(n) {
var rRoot = Math.sqrt(n),
var rRoot = Math.sqrt(n),
intRoot = Math.floor(rRoot),
intRoot = Math.floor(rRoot),

lows = range(1, intRoot)
lows = range(1, intRoot)
.filter(function (x) {
.filter(function (x) {
return (n % x) === 0;
return (n % x) === 0;
});
});

// for perfect squares, we can drop the head of the 'highs' list
return lows.concat(lows.map(function (x) {
return lows.concat(lows.map(function (x) {
return n / x;
return n / x;
Line 6,687: Line 6,646:
.slice((rRoot === intRoot) | 0));
.slice((rRoot === intRoot) | 0));
}
}

// range(intFrom, intTo, optional intStep)
// Int -> Int -> Maybe Int -> [Int]
function range(m, n, delta) {
function range(m, n, delta) {
var d = delta || 1,
var d = delta || 1,
Line 6,696: Line 6,652:
a = Array(lng),
a = Array(lng),
i = lng;
i = lng;

if (blnUp)
if (blnUp)
while (i--) a[i] = (d * i) + m;
while (i--) a[i] = (d * i) + m;
else
else
while (i--) a[i] = m - (d * i);
while (i--) a[i] = m - (d * i);

return a;
return a;
}
}

})(100);</lang>
})(100);</lang>

Or we can note, on inspection and further reflection, that only perfect squares have odd numbers of integer factors - all other numbers have only matched pairs of factors - low factors below the non-integer square root, and the corresponding quotients above the square root. In the case of perfect squares, the additional integer square root (not paired with any other factor than itself) makes the total number of distinct factors odd.
Or we can note, on inspection and further reflection, that only perfect squares have odd numbers of integer factors - all other numbers have only matched pairs of factors - low factors below the non-integer square root, and the corresponding quotients above the square root. In the case of perfect squares, the additional integer square root (not paired with any other factor than itself) makes the total number of distinct factors odd.

<lang JavaScript>(function (n) {
<lang JavaScript>(function (n) {
'use strict';
"use strict";

return perfectSquaresUpTo(100);
return perfectSquaresUpTo(100);

function perfectSquaresUpTo(n) {
function perfectSquaresUpTo(n) {
return range(1, Math.floor(Math.sqrt(n)))
return range(1, Math.floor(Math.sqrt(n)))
Line 6,720: Line 6,669:
});
});
}
}

// GENERIC

// range(intFrom, intTo, optional intStep)
// Int -> Int -> Maybe Int -> [Int]
function range(m, n, delta) {
function range(m, n, delta) {
var d = delta || 1,
var d = delta || 1,
Line 6,731: Line 6,675:
a = Array(lng),
a = Array(lng),
i = lng;
i = lng;

if (blnUp)
if (blnUp)
while (i--) a[i] = (d * i) + m;
while (i--) a[i] = (d * i) + m;
Line 6,738: Line 6,681:
return a;
return a;
}
}

})(100);</lang>
})(100);</lang>

{{Out}}
<lang JavaScript>[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]</lang>


=== ES6 ===
=== ES6 ===