Narcissistic decimal number: Difference between revisions

Added zkl
(→‎{{header|Perl}}: Added simple Perl 5 implementation)
(Added zkl)
Line 959:
<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|zkl}}==
<lang zkl>fcn isNarcissistic(n){
ns:=n.toString().split("").apply("toInt");
m:=ns.len()-1;
ns.reduce('wrap(s,d){ z:=d; do(m){z*=d} s+z },0) == n
}</lang>
Pre computing the first 15 powers of 0..9 for use as a look up table speeds things up quite a bit but performance is pretty underwhelming.
<lang zkl>var powers=(10).pump(List,'wrap(n){
(1).pump(15,List,'wrap(p){ n.toFloat().pow(p).toInt() })});
fcn isNarcissistic(n){
m:=(n.numDigits-1);
n.toString().reduce('wrap(s,c){ s+powers[c][m] },0) == n
};</lang>
Now stick a filter on a infinite lazy sequence (ie iterator) to create an infinite sequence of narcissistic numbers (iterator.filter(n,f) --> n results of f(i).toBool()==True).
<lang zkl>ns:=[0..].filter.fp1(isNarcissistic);
ns(15).println();
ns(5).println();
ns(5).println();</lang>
{{out}}
<pre>
L(0,1,2,3,4,5,6,7,8,9,153,370,371,407,1634)
L(8208,9474,54748,92727,93084)
L(548834,1741725,4210818,9800817,9926315)
</pre>
Anonymous user