2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,14 +1,9 @@
|
|||
(function () {
|
||||
// Array comprehension style
|
||||
[ for (i of Array.apply(null, { length: 100 })) i ].forEach((_, i) => {
|
||||
var door = i + 1
|
||||
var sqrt = Math.sqrt(door);
|
||||
|
||||
return rng(1, Math.sqrt(100)).map(function (x) {
|
||||
return x * x;
|
||||
});
|
||||
|
||||
// rng(1, 20) --> [1..20]
|
||||
function rng(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
if (sqrt === (sqrt | 0)) {
|
||||
console.log("Door %d is open", door);
|
||||
}
|
||||
|
||||
})();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1 +1,29 @@
|
|||
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
||||
(function (n) {
|
||||
|
||||
|
||||
// ONLY PERFECT SQUARES HAVE AN ODD NUMBER OF INTEGER FACTORS
|
||||
// (Leaving the door open at the end of the process)
|
||||
|
||||
return perfectSquaresUpTo(n);
|
||||
|
||||
|
||||
// perfectSquaresUpTo :: Int -> [Int]
|
||||
function perfectSquaresUpTo(n) {
|
||||
return range(1, Math.floor(Math.sqrt(n)))
|
||||
.map(x => x * x);
|
||||
}
|
||||
|
||||
|
||||
// GENERIC
|
||||
|
||||
// range(intFrom, intTo, optional intStep)
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
function range(m, n, step) {
|
||||
let d = (step || 1) * (n >= m ? 1 : -1);
|
||||
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, (_, i) => m + (i * d));
|
||||
}
|
||||
|
||||
})(100);
|
||||
|
|
|
|||
|
|
@ -1,9 +1 @@
|
|||
Array.apply(null, { length: 100 })
|
||||
.map((v, i) => i + 1)
|
||||
.forEach(door => {
|
||||
var sqrt = Math.sqrt(door);
|
||||
|
||||
if (sqrt === (sqrt | 0)) {
|
||||
console.log("Door %d is open", door);
|
||||
}
|
||||
});
|
||||
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
|
||||
|
|
|
|||
|
|
@ -1,41 +1,80 @@
|
|||
(function () {
|
||||
return chain(
|
||||
(function (n) {
|
||||
'use strict';
|
||||
|
||||
// 100 passes ...
|
||||
rng(0, 99).reduce(function (a, _, i) {
|
||||
return a.slice(0, i).concat(
|
||||
a.slice(i).map(function (v, j) {
|
||||
return (i + j + 1) % (i + 1) ? v : {
|
||||
door: v.door,
|
||||
open: !v.open
|
||||
};
|
||||
})
|
||||
)
|
||||
},
|
||||
|
||||
// 100 closed doors at start
|
||||
Array.apply(null, Array(100)).map(function (x, i) {
|
||||
return {
|
||||
open: false,
|
||||
door: i + 1
|
||||
};
|
||||
})),
|
||||
// finalDoors :: Int -> [(Int, Bool)]
|
||||
function finalDoors(n) {
|
||||
var lstRange = range(1, n);
|
||||
|
||||
// Filtering by chained function
|
||||
function (door) {
|
||||
return door.open ? [door] : [];
|
||||
return lstRange
|
||||
.reduce(function (a, _, k) {
|
||||
var m = k + 1;
|
||||
|
||||
return a.map(function (x, i) {
|
||||
var j = i + 1;
|
||||
|
||||
return [j, j % m ? x[1] : !x[1]];
|
||||
});
|
||||
}, zip(
|
||||
lstRange,
|
||||
replicate(n, false)
|
||||
));
|
||||
};
|
||||
|
||||
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// zip :: [a] -> [b] -> [(a,b)]
|
||||
function zip(xs, ys) {
|
||||
return xs.length === ys.length ? (
|
||||
xs.map(function (x, i) {
|
||||
return [x, ys[i]];
|
||||
})
|
||||
) : undefined;
|
||||
}
|
||||
)
|
||||
|
||||
// Monadic bind (chain) for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
// replicate :: Int -> a -> [a]
|
||||
function replicate(n, a) {
|
||||
var v = [a],
|
||||
o = [];
|
||||
|
||||
// range(1, 20) --> [1..20]
|
||||
function rng(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
})();
|
||||
if (n < 1) return o;
|
||||
while (n > 1) {
|
||||
if (n & 1) o = o.concat(v);
|
||||
n >>= 1;
|
||||
v = v.concat(v);
|
||||
}
|
||||
return o.concat(v);
|
||||
}
|
||||
|
||||
// range(intFrom, intTo, optional intStep)
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
function range(m, n, delta) {
|
||||
var d = delta || 1,
|
||||
blnUp = n > m,
|
||||
lng = Math.floor((blnUp ? n - m : m - n) / d) + 1,
|
||||
a = Array(lng),
|
||||
i = lng;
|
||||
|
||||
if (blnUp)
|
||||
while (i--) a[i] = (d * i) + m;
|
||||
else
|
||||
while (i--) a[i] = m - (d * i);
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
|
||||
return finalDoors(n)
|
||||
.filter(function (tuple) {
|
||||
return tuple[1];
|
||||
})
|
||||
.map(function (tuple) {
|
||||
return {
|
||||
door: tuple[0],
|
||||
open: tuple[1]
|
||||
};
|
||||
});
|
||||
|
||||
})(100);
|
||||
|
|
|
|||
|
|
@ -1,9 +1,44 @@
|
|||
Array.apply(null, { length: 100 })
|
||||
.map(function(v, i) { return i + 1; })
|
||||
.forEach(function(door) {
|
||||
var sqrt = Math.sqrt(door);
|
||||
(function (n) {
|
||||
'use strict';
|
||||
|
||||
if (sqrt === (sqrt | 0)) {
|
||||
console.log("Door %d is open", door);
|
||||
}
|
||||
});
|
||||
return range(1, 100)
|
||||
.filter(function (x) {
|
||||
return integerFactors(x)
|
||||
.length % 2;
|
||||
});
|
||||
|
||||
function integerFactors(n) {
|
||||
var rRoot = Math.sqrt(n),
|
||||
intRoot = Math.floor(rRoot),
|
||||
|
||||
lows = range(1, intRoot)
|
||||
.filter(function (x) {
|
||||
return (n % x) === 0;
|
||||
});
|
||||
|
||||
// for perfect squares, we can drop the head of the 'highs' list
|
||||
return lows.concat(lows.map(function (x) {
|
||||
return n / x;
|
||||
})
|
||||
.reverse()
|
||||
.slice((rRoot === intRoot) | 0));
|
||||
}
|
||||
|
||||
// range(intFrom, intTo, optional intStep)
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
function range(m, n, delta) {
|
||||
var d = delta || 1,
|
||||
blnUp = n > m,
|
||||
lng = Math.floor((blnUp ? n - m : m - n) / d) + 1,
|
||||
a = Array(lng),
|
||||
i = lng;
|
||||
|
||||
if (blnUp)
|
||||
while (i--) a[i] = (d * i) + m;
|
||||
else
|
||||
while (i--) a[i] = m - (d * i);
|
||||
|
||||
return a;
|
||||
}
|
||||
|
||||
})(100);
|
||||
|
|
|
|||
|
|
@ -1,34 +1,31 @@
|
|||
(function () {
|
||||
return chain(
|
||||
(function (n) {
|
||||
'use strict';
|
||||
|
||||
rng(1, 100),
|
||||
return perfectSquaresUpTo(100);
|
||||
|
||||
function (x) {
|
||||
var root = Math.sqrt(x);
|
||||
|
||||
return root === Math.floor(root) ? inject(x) : fail();
|
||||
function perfectSquaresUpTo(n) {
|
||||
return range(1, Math.floor(Math.sqrt(n)))
|
||||
.map(function (x) {
|
||||
return x * x;
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
// GENERIC
|
||||
|
||||
/*************************************************************/
|
||||
// range(intFrom, intTo, optional intStep)
|
||||
// Int -> Int -> Maybe Int -> [Int]
|
||||
function range(m, n, delta) {
|
||||
var d = delta || 1,
|
||||
blnUp = n > m,
|
||||
lng = Math.floor((blnUp ? n - m : m - n) / d) + 1,
|
||||
a = Array(lng),
|
||||
i = lng;
|
||||
|
||||
// monadic Bind/chain for lists
|
||||
function chain(xs, f) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
}
|
||||
if (blnUp)
|
||||
while (i--) a[i] = (d * i) + m;
|
||||
else
|
||||
while (i--) a[i] = m - (d * i);
|
||||
return a;
|
||||
}
|
||||
|
||||
// monadic Return/inject for lists
|
||||
function inject(x) { return [x]; }
|
||||
|
||||
// monadic Fail for lists
|
||||
function fail() { return []; }
|
||||
|
||||
// rng(1, 20) --> [1..20]
|
||||
function rng(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
});
|
||||
}
|
||||
|
||||
})();
|
||||
})(100);
|
||||
|
|
|
|||
|
|
@ -1,18 +1,9 @@
|
|||
(function () {
|
||||
Array.apply(null, { length: 100 })
|
||||
.map((v, i) => i + 1)
|
||||
.forEach(door => {
|
||||
var sqrt = Math.sqrt(door);
|
||||
|
||||
return rng(1, 100).filter(
|
||||
function (x) {
|
||||
var root = Math.sqrt(x);
|
||||
|
||||
return root === Math.floor(root);
|
||||
}
|
||||
);
|
||||
|
||||
// rng(1, 20) --> [1..20]
|
||||
function rng(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(function (x, i) {
|
||||
return m + i;
|
||||
if (sqrt === (sqrt | 0)) {
|
||||
console.log("Door %d is open", door);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue