Narcissistic decimal number: Difference between revisions

Content added Content deleted
Line 1,199: Line 1,199:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
===ES5===
{{trans|Java}}
{{trans|Java}}
<lang javascript>function isNarc(x) {
<lang javascript>function isNarc(x) {
Line 1,226: Line 1,227:
{{out}}
{{out}}
<pre>"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"</pre>
<pre>"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"</pre>

===ES6===

<lang JavaScript>(() => {
'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) => {
return 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
.join('\n');
})();</lang>

{{Out}}
<pre>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</pre>


=={{header|jq}}==
=={{header|jq}}==