Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
|
|
@ -0,0 +1,95 @@
|
|||
(function () {
|
||||
'use strict';
|
||||
|
||||
// GENERIC FUNCTIONS
|
||||
|
||||
// concatMap :: (a -> [b]) -> [a] -> [b]
|
||||
var concatMap = function concatMap(f, xs) {
|
||||
return [].concat.apply([], xs.map(f));
|
||||
},
|
||||
|
||||
// curry :: ((a, b) -> c) -> a -> b -> c
|
||||
curry = function curry(f) {
|
||||
return function (a) {
|
||||
return function (b) {
|
||||
return f(a, b);
|
||||
};
|
||||
};
|
||||
},
|
||||
|
||||
// intersectBy :: (a - > a - > Bool) - > [a] - > [a] - > [a]
|
||||
intersectBy = function intersectBy(eq, xs, ys) {
|
||||
return xs.length && ys.length ? xs.filter(function (x) {
|
||||
return ys.some(curry(eq)(x));
|
||||
}) : [];
|
||||
},
|
||||
|
||||
// range :: Int -> Int -> Maybe Int -> [Int]
|
||||
range = function range(m, n, step) {
|
||||
var d = (step || 1) * (n >= m ? 1 : -1);
|
||||
return Array.from({
|
||||
length: Math.floor((n - m) / d) + 1
|
||||
}, function (_, i) {
|
||||
return m + i * d;
|
||||
});
|
||||
};
|
||||
|
||||
// PROBLEM FUNCTIONS
|
||||
|
||||
// add, mul :: (Int, Int) -> Int
|
||||
var add = function add(xy) {
|
||||
return xy[0] + xy[1];
|
||||
},
|
||||
mul = function mul(xy) {
|
||||
return xy[0] * xy[1];
|
||||
};
|
||||
|
||||
// sumEq, mulEq :: (Int, Int) -> [(Int, Int)]
|
||||
var sumEq = function sumEq(p) {
|
||||
var addP = add(p);
|
||||
return s1.filter(function (q) {
|
||||
return add(q) === addP;
|
||||
});
|
||||
},
|
||||
mulEq = function mulEq(p) {
|
||||
var mulP = mul(p);
|
||||
return s1.filter(function (q) {
|
||||
return mul(q) === mulP;
|
||||
});
|
||||
};
|
||||
|
||||
// pairEQ :: ((a, a) -> (a, a)) -> Bool
|
||||
var pairEQ = function pairEQ(a, b) {
|
||||
return a[0] === b[0] && a[1] === b[1];
|
||||
};
|
||||
|
||||
// MAIN
|
||||
|
||||
// xs :: [Int]
|
||||
var xs = range(1, 100);
|
||||
|
||||
// s1 s2, s3, s4 :: [(Int, Int)]
|
||||
var s1 = concatMap(function (x) {
|
||||
return concatMap(function (y) {
|
||||
return 1 < x && x < y && x + y < 100 ? [
|
||||
[x, y]
|
||||
] : [];
|
||||
}, xs);
|
||||
}, xs),
|
||||
|
||||
s2 = s1.filter(function (p) {
|
||||
return sumEq(p).every(function (q) {
|
||||
return mulEq(q).length > 1;
|
||||
});
|
||||
}),
|
||||
|
||||
s3 = s2.filter(function (p) {
|
||||
return intersectBy(pairEQ, mulEq(p), s2).length === 1;
|
||||
}),
|
||||
|
||||
s4 = s3.filter(function (p) {
|
||||
return intersectBy(pairEQ, sumEq(p), s3).length === 1;
|
||||
});
|
||||
|
||||
return s4;
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
[[4, 13]]
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
(() => {
|
||||
"use strict";
|
||||
|
||||
// ------------- SUM AND PRODUCT PUZZLE --------------
|
||||
|
||||
// main :: IO ()
|
||||
const main = () => {
|
||||
const
|
||||
// xs :: [Int]
|
||||
xs = enumFromTo(1)(100),
|
||||
|
||||
// s1 s2, s3, s4 :: [(Int, Int)]
|
||||
s1 = xs.flatMap(
|
||||
x => xs.flatMap(y =>
|
||||
(1 < x) && (x < y) && 100 > (x + y) ? [
|
||||
[x, y]
|
||||
] : []
|
||||
)
|
||||
),
|
||||
s2 = s1.filter(
|
||||
p => sumEq(p, s1).every(
|
||||
q => 1 < mulEq(q, s1).length
|
||||
)
|
||||
),
|
||||
s3 = s2.filter(
|
||||
p => 1 === intersectBy(pairEQ)(
|
||||
mulEq(p, s1)
|
||||
)(s2).length
|
||||
);
|
||||
|
||||
return s3.filter(
|
||||
p => 1 === intersectBy(pairEQ)(
|
||||
sumEq(p, s1)
|
||||
)(s3).length
|
||||
);
|
||||
};
|
||||
|
||||
// ---------------- PROBLEM FUNCTIONS ----------------
|
||||
|
||||
// add, mul :: (Int, Int) -> Int
|
||||
const
|
||||
add = xy => xy[0] + xy[1],
|
||||
mul = xy => xy[0] * xy[1],
|
||||
|
||||
// sumEq, mulEq :: (Int, Int) ->
|
||||
// [(Int, Int)] -> [(Int, Int)]
|
||||
sumEq = (p, s) => {
|
||||
const addP = add(p);
|
||||
|
||||
return s.filter(q => add(q) === addP);
|
||||
},
|
||||
mulEq = (p, s) => {
|
||||
const mulP = mul(p);
|
||||
|
||||
return s.filter(q => mul(q) === mulP);
|
||||
},
|
||||
|
||||
// pairEQ :: ((a, a) -> (a, a)) -> Bool
|
||||
pairEQ = a => b => (
|
||||
a[0] === b[0]
|
||||
) && (a[1] === b[1]);
|
||||
|
||||
|
||||
// ---------------- GENERIC FUNCTIONS ----------------
|
||||
|
||||
// enumFromTo :: Int -> Int -> [Int]
|
||||
const enumFromTo = m =>
|
||||
n => Array.from({
|
||||
length: 1 + n - m
|
||||
}, (_, i) => m + i);
|
||||
|
||||
|
||||
// intersectBy :: (a -> a -> Bool) -> [a] -> [a] -> [a]
|
||||
const intersectBy = eqFn =>
|
||||
// The intersection of the lists xs and ys
|
||||
// in terms of the equality defined by eq.
|
||||
xs => ys => xs.filter(
|
||||
x => ys.some(eqFn(x))
|
||||
);
|
||||
|
||||
// MAIN ---
|
||||
return main();
|
||||
})();
|
||||
|
|
@ -0,0 +1 @@
|
|||
[[4, 13]]
|
||||
Loading…
Add table
Add a link
Reference in a new issue