Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,24 @@
function isNarc(x) {
var str = x.toString(),
i,
sum = 0,
l = str.length;
if (x < 0) {
return false;
} else {
for (i = 0; i < l; i++) {
sum += Math.pow(str.charAt(i), l);
}
}
return sum == x;
}
function main(){
var n = [];
for (var x = 0, count = 0; count < 25; x++){
if (isNarc(x)){
n.push(x);
count++;
}
}
return n.join(' ');
}

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,144 @@
(() => {
'use strict';
// 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 =>
0 < n ? filter(isDaffodil(n))(
digitPowerSums(n)
) : [0];
// 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:
// (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) => 0 < n ? (
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 -> [Int]
const enumFromTo = m => n =>
Array.from({
length: 1 + n - m
}, (_, i) => m + i);
// concatMap :: (a -> [b]) -> [a] -> [b]
const concatMap = f =>
xs => xs.flatMap(f);
// cons :: a -> [a] -> [a]
const cons = x =>
xs => [x].concat(xs);
// 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);
// map :: (a -> b) -> [a] -> [b]
const map = f =>
xs => xs.map(f);
// isNull :: [a] -> Bool
// 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;
// take :: Int -> [a] -> [a]
const take = n =>
xs => xs.slice(0, n);
// snd :: (a, b) -> b
const snd = tpl => tpl[1];
// show :: a -> String
const show = x => JSON.stringify(x)
// 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]));
// ------------------------FORMATTING-------------------------
// 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();
})();