The speed of the is_prime function can be increase by about 140% by using the 2 4 trick used below.
sub is_prime {
my %prime = map { $_ => 1 } (2, 3, 5, 7);
my %not_prime;
return sub {
my $n = shift;
return 1 if $prime{ $n };
return 0 if $n % 2 == 0  $n % 3 == 0
 exists $not_prime{ $n };
my $last = int sqrt $n;
for ( my $x = 5; $x <= $last ;) {
return $not_prime{ $n } = 0 if $n % $x == 0;
$x += 2;
return $not_prime{ $n } = 0 if $n % $x == 0;
$x += 4;
}
return $prime{ $n } = 1;
};
}
This version skips all of the numbers that are divisable by
2 and 3. Not checking numbers divisable by 2 is obvious, but not checking numbers divisable by 3 is less so, but reduces the search space by 25%.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
 a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)

For: 

Use: 
 &   & 
 <   < 
 >   > 
 [   [ 
 ]   ] 
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.

