Family Day update

This commit is contained in:
Ingy döt Net 2020-02-17 23:21:07 -08:00
parent aac6731f2c
commit 9ad63ea473
2442 changed files with 39761 additions and 8255 deletions

View file

@ -1,24 +1,37 @@
(() => {
'use strict';
// DAFFODILS --------------------------------------------------------------
// main :: IO ()
const main = () =>
console.log(
fTable(
'Narcissistic decimal numbers of lengths [1..7]:\n'
)(show)(show)(
narcissiOfLength
)(enumFromTo(1)(7))
);
// narcissiOfLength :: Int -> [Int]
const narcissiOfLength = n =>
n > 0 ? filter(curry(isDaffodil)(n), digitPowerSums(n)) : [0];
0 < n ? filter(isDaffodil(n))(
digitPowerSums(n)
) : [0];
// Do the decimal digits of N, each raised to the power E, sum to N itself ?
// isDaffodil :: Int -> Int -> Bool
const isDaffodil = (e, n) => {
const
powerSum = (n, xs) => xs.reduce((a, x) => a + Math.pow(x, n), 0),
digitList = n => (n > 0) ? (
cons((n % 10), digitList(Math.floor(n / 10)))
) : [],
ds = digitList(n);
return e === ds.length && n === powerSum(e, ds);
};
// powerSum :: Int -> [Int] -> Int
const powerSum = n =>
xs => xs.reduce(
(a, x) => a + pow(x, n), 0
);
// isDaffodil :: Int -> Int -> Bool
const isDaffodil = e => n => {
// True if the decimal digits of N,
// each raised to the power E, sum to N.
const ds = digitList(n);
return e === ds.length && n === powerSum(e)(ds);
};
// The subset of integers of n digits that actually need daffodil checking:
@ -28,80 +41,104 @@ const isDaffodil = (e, n) => {
// digitPowerSums :: Int -> [Int]
const digitPowerSums = nDigits => {
const
digitPowers = map(x => [x, pow(x, nDigits)], enumFromTo(0, 9)),
treeGrowth = (n, parentPairs) => (n > 0) ? (
digitPowers = map(x => [x, pow(x, nDigits)])(
enumFromTo(0)(9)
),
treeGrowth = (n, parentPairs) => 0 < n ? (
treeGrowth(n - 1,
isNull(parentPairs) ? (
digitPowers
) : concatMap(([parentDigit, parentSum]) =>
) : concatMap(
([parentDigit, parentSum]) =>
map(([leafDigit, leafSum]) => //
[leafDigit, parentSum + leafSum],
take(parentDigit + 1, digitPowers)
),
parentPairs
))
[leafDigit, parentSum + leafSum])(
take(parentDigit + 1)(digitPowers)
)
)(parentPairs)
)
) : parentPairs;
return map(snd, treeGrowth(nDigits, []));
return map(snd)(treeGrowth(nDigits, []));
};
// GENERIC FUNCTIONS ------------------------------------------------------
// enumFromTo :: Int -> Int -> Maybe Int -> [Int]
const enumFromTo = (m, n, step) => {
const d = (step || 1) * (n >= m ? 1 : -1);
return Array.from({
length: Math.floor((n - m) / d) + 1
}, (_, i) => m + (i * d));
};
// ---------------------GENERIC FUNCTIONS---------------------
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = m => n =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
const concatMap = f =>
xs => xs.flatMap(f);
// cons :: a -> [a] -> [a]
const cons = (x, xs) => [x].concat(xs);
const cons = x =>
xs => [x].concat(xs);
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
const go = xs => xs.length >= f.length ? (f.apply(null, xs)) :
function () {
return go(xs.concat([].slice.apply(arguments)));
};
return go([].slice.call(args, 1));
};
// digitList :: Int -> [Int]
const digitList = n => {
const go = x => 0 < x ? (
cons(x % 10)(
go(Math.floor(x / 10))
)
) : [];
return 0 < n ? go(n) : [0];
}
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
const filter = f => xs => xs.filter(f);
// map :: (a -> b) -> [a] -> [b]
const map = curry((f, xs) => xs.map(f));
const map = f =>
xs => xs.map(f);
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
// isNull :: String -> Bool
const isNull = xs =>
1 > xs.length;
// length :: [a] -> Int
const length = xs => xs.length;
// pow :: Int -> Int -> Int
const pow = Math.pow
const pow = Math.pow;
// take :: Int -> [a] -> [a]
const take = (n, xs) => xs.slice(0, n);
// show ::
// (a -> String) f, Num n =>
// a -> maybe f -> maybe n -> String
const show = JSON.stringify;
const take = n =>
xs => xs.slice(0, n);
// snd :: (a, b) -> b
const snd = tpl => Array.isArray(tpl) ? tpl[1] : undefined;
const snd = tpl => tpl[1];
// show :: a -> String
const show = x => JSON.stringify(x)
// TEST -------------------------------------------------------------------
// zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
const zipWith = f => xs => ys =>
xs.slice(
0, Math.min(xs.length, ys.length)
).map((x, i) => f(x)(ys[i]));
// return length(concatMap(digitPowerSums, enumFromTo(0, 7)));
// ------------------------FORMATTING-------------------------
return show(
//digitPowerSums(3)
concatMap(narcissiOfLength, enumFromTo(0, 7))
);
// fTable :: String -> (a -> String) -> (b -> String)
// -> (a -> b) -> [a] -> String
const fTable = s => xShow => fxShow => f => xs => {
// Heading -> x display function ->
// fx display function ->
// f -> values -> tabular string
const
ys = xs.map(xShow),
w = Math.max(...ys.map(length));
return s + '\n' + zipWith(
a => b => a.padStart(w, ' ') + ' -> ' + b
)(ys)(
xs.map(x => fxShow(f(x)))
).join('\n');
};
// MAIN ---
return main();
})();