Narcissistic decimal number: Difference between revisions

Content added Content deleted
(→‎Haskell reduced search: (used swap in digitList))
(→‎JS ES6: -> Generating the unordered digit combinations as their power sums)
Line 1,544:
 
In this way we can find the 25th narcissistic number after '''sum(map(compose(length, digitGroups), enumFromTo(1, 7))) === 19447''' tests – an improvement on the exhaustive trawl through '''9926315''' integers.
 
(Generating the unordered digit combinations directly as power sums allows faster testing later, and needs less space)
<lang JavaScript>(() => {
'use strict';
 
// DAFFODILS --------------------------------------------------------------
// GENERIC FUNCTIONS
 
// arrayEqnarcissiOfLength :: [a]Int -> [aInt] -> Bool
const arrayEqnarcissiOfLength = (xs, ys)n => {
n > 0 ? filter(curry(isDaffodil)(n), digitPowerSums(n)) : [0];
const lngX = xs.length;
 
return (lngX !== ys.length) ? false : (
// Do the decimal digits of N, each raised to the power E, sum to N itself ?
lngX > 0 ? (
 
(() => {
// isDaffodil :: Int -> Int -> Bool
let i = lngX;
const isDaffodil = (e, n) => {
while (i--)
const
if (xs[i] !== ys[i]) return false;
powerSum = (n, xs) => xs.reduce((a, x) => returna true;+ Math.pow(x, n), 0),
digitList = n => })(n > 0) ? (
cons((n % 10), :digitList(Math.floor(n true/ 10)))
); : [],
ds = digitList(n);
return e === ds.length && n === powerSum(e, ds);
};
 
// The subset of integers of n digits that actually need daffodil checking:
// concat :: [[a]] -> [a] | [String] -> String
 
const concat = xs => {
// (Flattened leaves of a tree of unique digit combinations, in which
if (xs.length > 0) {
// order is not significant. Digit sequence doesn't affect power summing)
const unit = typeof xs[0] === 'string' ? '' : [];
 
return unit.concat.apply(unit, xs);
// digitPowerSums :: Int } else return-> [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));
Line 1,578 ⟶ 1,607:
const cons = (x, xs) => [x].concat(xs);
 
// 2 or more arguments
// curry :: Function -> Function
const curry = (f, ...args) => {
Line 1,586 ⟶ 1,616:
return go([].slice.call(args, 1));
};
 
// enumFromTo :: Int -> Int -> [Int]
const enumFromTo = (m, n) =>
Array.from({
length: Math.floor(n - m) + 1
}, (_, i) => m + i);
 
// filter :: (a -> Bool) -> [a] -> [a]
const filter = (f, xs) => xs.filter(f);
 
// headmap :: (a -> b) -> [a] -> a[b]
const headmap = curry((f, xs) => xs.length ? xs[0] : undefinedmap(f));
 
// isNull :: [a] -> Bool
const isNull = xs => (xs instanceof Array) ? xs.length < 1 : undefined;
 
// maplength :: (a -> b) -> [a] -> [b]Int
const maplength = curry((f, xs) => xs.map(f))length;
 
// pow :: Int -> Int -> Int
const pow = Math.pow
 
// puretake :: Int -> [a] -> [a]
const puretake = x(n, xs) => [x]xs.slice(0, n);
 
// show :: a -> String
// (a -> String) f, Num n =>
// a -> maybe f -> maybe n -> String
const show = JSON.stringify;
 
// sortsnd :: Ord (a, => [a]b) -> [a]b
const sortsnd = xstpl => xsArray.sortisArray(tpl) ? tpl[1] : undefined;
 
 
// NARCISSI
 
// narcissiOfLength :: Int -> [Int]
const narcissiOfLength = n => {
const isDaffodil = sortedDigits =>
arrayEq(sortedDigits, sort(digitList(powerSum(n, sortedDigits))));
return map(powerSum(n), filter(isDaffodil, digitGroups(n)));
};
 
// powerSum :: Int -> [Int] -> Int
const powerSum = curry((n, xs) =>
xs.reduce((a, x) => a + Math.pow(x, n), 0));
 
// Full list of sorted combinations of digits [0..9], with repetition.
 
// digitGroups :: Int -> [[Int]]
const digitGroups = nDigits => {
const
digits = enumFromTo(0, 9),
sortedCombinations = (n, xs) => (n > 0) ? (
sortedCombinations(n - 1,
isNull(xs) ? (
map(pure, digits)
) : concatMap(xxs =>
map(y => cons(y, xxs), enumFromTo(0, head(xxs))), xs
))
) : xs;
return sortedCombinations(nDigits, []);
};
 
// digitList :: Int -> [Int]
const digitList = n =>
(n > 0) ? cons((n % 10), digitList(Math.floor(n / 10))) : [];
 
// TEST -------------------------------------------------------------------
 
// return length(concatMap(digitPowerSums, enumFromTo(0, 7)));
// TEST
 
// Narcissistic decimal numbers of digit length from 1 to 7:
return show(
concat(cons(0, mapconcatMap(narcissiOfLength, enumFromTo(10, 7))))
);
})();</lang>