September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -0,0 +1,69 @@
(() => {
'use strict';
// HARSHADS ---------------------------------------------------------------
// nHarshads :: Int -> [Int]
const nHarshads = n => {
// isHarshad :: Int -> Bool
const isHarshad = n => 0 === n % sum(digitList(n));
return until(
dct => dct.nth === n,
dct => {
const
next = succ(dct.i),
blnHarshad = isHarshad(next);
return {
i: next,
hs: blnHarshad ? dct.hs.concat(next) : dct.hs,
nth: dct.nth + (blnHarshad ? 1 : 0)
};
}, {
i: 0,
hs: [],
nth: 0
}
)
.hs;
};
// GENERIC FUNCTIONS ------------------------------------------------------
// digitList :: Int -> [Int]
const digitList = n =>
n > 0 ? [n % 10].concat(digitList(Math.floor(n / 10))) : [];
// dropWhile :: (a -> Bool) -> [a] -> [a]
const dropWhile = (p, xs) => {
let i = 0;
for (let lng = xs.length;
(i < lng) && p(xs[i]); i++) {}
return xs.slice(i);
};
// head :: [a] -> a
const head = xs => xs.length ? xs[0] : undefined;
// a -> String
const show = x => JSON.stringify(x, null, 2);
// succ :: Int -> Int
const succ = x => x + 1
// sum :: (Num a) => [a] -> a
const sum = xs => xs.reduce((a, x) => a + x, 0);
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
const go = x => p(x) ? x : go(f(x));
return go(x);
};
// TEST -------------------------------------------------------------------
return show({
firstTwenty: nHarshads(20),
firstOver1000: head(dropWhile(x => x <= 1000, nHarshads(1000)))
});
})();