Narcissistic decimal number: Difference between revisions

m
→‎{{header|Raku}}: split versions apart, add concurrency to 1st
m (→‎{{header|Perl}}: now with v5.36, add output)
m (→‎{{header|Raku}}: split versions apart, add concurrency to 1st)
Line 4,329:
=={{header|Raku}}==
(formerly Perl 6)
 
Here is a straightforward, naive implementation. It works but takes ages.
===Simple, with concurrency===
Simple implementation is not exactly speedy, but concurrency helps move things along.
<lang perl6>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars }
my @N = lazy if(0..∞).hyper.grep: *.&is-narcissistic {;
 
@N[^25].join(' ').say;</lang>
for 0 .. * {
if .&is-narcissistic {
.say;
last if ++state$ >= 25;
}
}</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>
<pre>0
1
2
3
4
5
6
7
8
9
153
370
371
407
Ctrl-C</pre>
 
Here the program was interrupted but if you're patient enough you'll see all the 25 numbers.
 
===Single-threaded, with precalculations===
Here's a faster version that precalculates the values for base 1000 digits:
This version that precalculates the values for base 1000 digits, but despite the extra work ends up taking more wall-clock time than the simpler version.
<lang perl6>sub kigits($n) {
my int $i = $n;
2,392

edits