Magnanimous numbers: Difference between revisions

Content added Content deleted
m (Swift - simplified code)
(→‎{{header|Raku}}: various style / efficiency tweaks)
Line 916: Line 916:
{{works with|Rakudo|2020.02}}
{{works with|Rakudo|2020.02}}


<lang perl6>my @magnanimous = lazy flat ^10, (10 .. 1001).hyper.map( {
<lang perl6>my @magnanimous = lazy flat ^10, (10 .. 1001).map( {
my $last;
my int $last;
(1 .. .chars - 1).map: -> \c { ++$last and last unless (.substr(0,c) + .substr(c)).is-prime }
(1 ..^ .chars).map: -> \c { $last = 1 and last unless (.substr(0,c) + .substr(c)).is-prime }
next if $last;
next if $last;
$_
$_
} ),
} ),


(1002 .. ∞).hyper.map: {
(1002 .. ∞).map: {
# optimization for numbers > 1001; First and last digit can not both be even or both be odd
# optimization for numbers > 1001; First and last digit can not both be even or both be odd
next if (.substr(0,1) % 2 + .substr(*-1) % 2) %% 2;
next if (.substr(0,1) + .substr(*-1)) %% 2;
my $last;
my int $last;
(1 .. .chars - 1).map: -> \c { ++$last and last unless (.substr(0,c) + .substr(c)).is-prime }
(1 ..^ .chars).map: -> \c { $last = 1 and last unless (.substr(0,c) + .substr(c)).is-prime }
next if $last;
next if $last;
$_
$_