Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,24 @@
function isHarshad(n) {
var s = 0;
var n_str = new String(n);
for (var i = 0; i < n_str.length; ++i) {
s += parseInt(n_str.charAt(i));
}
return n % s === 0;
}
var count = 0;
var harshads = [];
for (var n = 1; count < 20; ++n) {
if (isHarshad(n)) {
count++;
harshads.push(n);
}
}
console.log(harshads.join(" "));
var h = 1000;
while (!isHarshad(++h));
console.log(h);

View file

@ -0,0 +1,17 @@
function* harshads (start) {
for (let n = start; true; n++) {
const sum = [...n.toString()].map(Number).reduce((a, b) => a + b)
if (n % sum === 0) {
yield n
}
}
}
const first20 = (() => {
const hs = harshads(1)
return [...Array(20)].map(() => hs.next().value)
})()
console.log("First 20:", ...first20)
const firstAfter1000 = harshads(1001).next().value
console.log("First after 1000:", firstAfter1000)

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)))
});
})();