Largest palindrome product: Difference between revisions

→‎{{header|Raku}}: use external library for 25x speedup (still slow, but not dreadful)
(→‎{{header|Go}}: Updated as per Wren.)
(→‎{{header|Raku}}: use external library for 25x speedup (still slow, but not dreadful))
Line 75:
 
=={{header|Raku}}==
<lang perl6>use Prime::Factor;
 
<lang perl6>use PrimeInline::FactorPerl5;
.say for (1..11).hyper(:1batch).map: {.&lpp};
my $p5 = Inline::Perl5.new();
$p5.use: 'ntheory';
my &divisors = $p5.run('sub { ntheory::divisors $_[0] }');
 
.say for (12..11).hyper(:1batch12).map: {.&lpp};
multi lpp ($oom where {$_ +& 1}) { # even number of multiplicand digits
 
my $f = +(9 x ($oom + 1));
multi lpp ($oom where {!($_ +& 1)}) { # even number of multiplicand digits
my $o = (1 + $oom) / 2;
my $palf = +(9 x $o ~ 0 x $o * 2 ~ 9 x $ooom);
my $fo = +(9 x ($oom +/ 1))2;
sprintf "Largest palindromic product of two %2d-digit integers: %d × %d = %d", $oom + 1, $pal div $f, $f, $pal
my $opal = +(19 +x $o ~ 0 x $oom) /~ 29 x $o);
sprintf "Largest palindromic product of two %2d-digit integers: %d × %d = %d", $oom + 1, $pal div $f, $f, $pal
}
 
multi lpp ($oom where {$_ +^& 1}) { # odd number of multiplicand digits
my $p;
(+(1 ~ (0 x ($oom - 1))) .. +(9 ~ (9 x ($oom - 1)))).reverse.map({ +($_ ~ .flip) }).map: -> $pal {
for my @factors = divisors("$pal")».&divisorsInt.grep({ .chars == ($oom + 1)}).sort( -* ) {
next unless $pal div $_ ∈ @factors;
$p = sprintf("Largest palindromic product of two %2d-digit integers: %d × %d = %d", $oom + 1, $pal div $_, $_, $pal) and last;
last;
}
last if $p;
10,327

edits