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,40 @@
(() => {
'use strict';
// digits :: Int -> [Int]
const digits = n => n.toString()
.split('')
.map(x => parseInt(x, 10));
// pow :: Int -> Int -> Int
const pow = Math.pow;
// isNarc :: Int -> Bool
const isNarc = n => {
const
ds = digits(n),
len = ds.length;
return ds.reduce((a, x) =>
a + pow(x, len), 0) === n;
};
// until :: (a -> Bool) -> (a -> a) -> a -> a
const until = (p, f, x) => {
let v = x;
while (!p(v)) v = f(v);
return v;
};
return until(
x => x.narc.length > 24,
x => ({
n: x.n + 1,
narc: (isNarc(x.n) ? x.narc.concat(x.n) : x.narc)
}), {
n: 0,
narc: []
}
)
.narc
})();

View file

@ -0,0 +1 @@
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315]

View file

@ -0,0 +1,107 @@
(() => {
'use strict';
// DAFFODILS --------------------------------------------------------------
// narcissiOfLength :: Int -> [Int]
const narcissiOfLength = n =>
n > 0 ? filter(curry(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);
};
// The subset of integers of n digits that actually need daffodil checking:
// (Flattened leaves of a tree of unique digit combinations, in which
// order is not significant. Digit sequence doesn't affect power summing)
// digitPowerSums :: Int -> [Int]
const digitPowerSums = nDigits => {
const
digitPowers = map(x => [x, pow(x, nDigits)], enumFromTo(0, 9)),
treeGrowth = (n, parentPairs) => (n > 0) ? (
treeGrowth(n - 1,
isNull(parentPairs) ? (
digitPowers
) : concatMap(([parentDigit, parentSum]) =>
map(([leafDigit, leafSum]) => //
[leafDigit, parentSum + leafSum],
take(parentDigit + 1, digitPowers)
),
parentPairs
))
) : parentPairs;
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));
};
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = (f, xs) => [].concat.apply([], xs.map(f));
// cons :: a -> [a] -> [a]
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));
};
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
// map :: (a -> b) -> [a] -> [b]
const map = curry((f, xs) => xs.map(f));
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
// length :: [a] -> Int
const length = xs => xs.length;
// pow :: Int -> Int -> Int
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;
// snd :: (a, b) -> b
const snd = tpl => Array.isArray(tpl) ? tpl[1] : undefined;
// TEST -------------------------------------------------------------------
// return length(concatMap(digitPowerSums, enumFromTo(0, 7)));
return show(
//digitPowerSums(3)
concatMap(narcissiOfLength, enumFromTo(0, 7))
);
})();