use strict; use warnings; use Benchmark qw(:all) ; my $m = 1e7; cmpthese ( 5, { Op => sub { op(); }, Ath => sub { ath(); }, } ); sub op { my @c; my $last = 2; for (2 .. $m) { for (my $a = $_ * 2; $a <= $m; $a += $_) { $c[$a]++ } $last = $_ if !$c[$_]; } print "$last\n"; } sub ath { my @d; my $last = 2; for (my $a = 3; $a <= $m; $a += 2) { next if $d[$a]; for (my $b = $a * $a; $b <= $m; $b += $a) { $d[$b]++ } $last = $a; } print "$last\n"; }