Largest prime factor: Difference between revisions

→‎{{header|Raku}}: add example of much faster version using external library
(Added XPL0 example.)
(→‎{{header|Raku}}: add example of much faster version using external library)
Line 127:
 
=={{header|Raku}}==
Note: These are both extreme overkill for the task requirements.
Pure Raku, not particularly fast.
 
Pure Raku, not===Not particularly fast.===
Pure Raku. Using [https://modules.raku.org/search/?q=Prime%3A%3AFactor Prime::Factor] from the [https://modules.raku.org/ Raku ecosystem]. Makes it to 2^95 - 1 in 1 second on my system.
<lang perl6>use Prime::Factor;
 
Line 133 ⟶ 136:
 
for flat 600851475143, (1..∞).map: { 2 +< $_ - 1 } {
say "Largest prime factor of $_: ", .&max prime-factors.max $_;
exitlast if now - $start > 1.0; # quit after one second of total run time
}</lang>
 
Line 229 ⟶ 232:
Largest prime factor of 2475880078570760549798248447: 23140471537
Largest prime factor of 4951760157141521099596496895: 2796203
Largest prime factor of 9903520314283042199192993791: 658812288653553079</pre>
Largest prime factor of 19807040628566084398385987583: 165768537521
Largest prime factor of 39614081257132168796771975167: 30327152671</pre>
 
===Particularly fast===
Using [[:Category:Perl|Perl 5]] [[:Category:ntheory|ntheory]] library via [https://modules.raku.org/search/?q=Inline%3A%3APerl5 Inline::Perl5]. Makes it to about 2^155 - 1 in 1 second on my system. ''Varies from 2^145-1 (lowest seen) to 2^168-1 (highest seen).''
<lang perl6>use Inline::Perl5;
my $p5 = Inline::Perl5.new();
$p5.use: 'ntheory';
my &lpf = $p5.run('sub { ntheory::todigitstring ntheory::vecmax ntheory::factor $_[0] }');
 
my $start = now;
 
for flat 600851475143, (1..∞).map: { 2 +< $_ - 1 } {
say "Largest prime factor of $_: ", lpf "$_";
last if now - $start > 1; # quit after one second of total run time
}</lang>
Same output only much longer.
 
=={{header|Ring}}==
10,327

edits