Narcissistic decimal number: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Perl}}: now with v5.36, add output) |
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: split versions apart, add concurrency to 1st) |
||
Line 4,329: | Line 4,329: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(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 } |
<lang perl6>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars } |
||
⚫ | |||
@N[^25].join(' ').say;</lang> |
|||
for 0 .. * { |
|||
⚫ | |||
.say; |
|||
last if ++state$ >= 25; |
|||
} |
|||
}</lang> |
|||
{{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 |
|||
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) { |
<lang perl6>sub kigits($n) { |
||
my int $i = $n; |
my int $i = $n; |