why aren't you eliminating the numbers which end with 2, 4, 5, 6, 8, and 0 before doing any math

If you want you can, but how do you spot those numbers? you are using a regex that, as you can guess, is slower than math checks. Theese numbers will be catched with the modulo operator anyway, as choroba explained.
Also the regex in the original code is also not correct: you skip 2 and 5.
perl -e "map {print qq($_ ) unless $_ =~/(2|4|5|6|8|0)$/ } @ARGV" 2 3 +5 7 11 13 17 19 23 29 37 3 7 11 13 17 19 23 29 37
i used a different regex (and precompiled in the bench program using qr'\d[245680]$'):
perl -e "map {print qq($_ ) unless $_ =~/\d[245680]$/ } @ARGV" 2 3 5 7 + 11 13 17 19 23 29 37 2 3 5 7 11 13 17 19 23 29 37
About saving time skipping numbers ending with some fixed digit, as said, the regex solution is slower: with this code:
use strict; use warnings; use Benchmark qw{ cmpthese }; my $skipper_regex = qr'\d[245680]$'; sub primes_original { my $n = shift; return if $n < 2; my @primes = (2); for my $i (3 .. $n) { my $sqrt = sqrt $i; my $notprime; for my $p (@primes) { last if $p > $sqrt; $notprime = 1, last if 0 == $i % $p; } push @primes, $i unless $notprime; } return @primes } sub primes_skipper { my $n = shift; return if $n < 2; my @primes = (2); for my $i (3 .. $n) { next if $i =~ $skipper_regex; my $sqrt = sqrt $i; my $notprime; for my $p (@primes) { last if $p > $sqrt; $notprime = 1, last if 0 == $i % $p; } push @primes, $i unless $notprime; } return @primes } ## sub primes_opt_till_11 { my $n = shift; return if $n < 2; my @primes = (2); for my $i (3 .. $n) { if($i > 2 || $i % 2 == 0){next} if($i > 3 || $i % 3 == 0){next} if($i > 5 || $i % 5 == 0){next} if($i > 7 || $i % 7 == 0){next} if($i > 11 || $i % 11 == 0){next} my $sqrt = sqrt $i; my $notprime; for my $p (@primes) { last if $p > $sqrt; $notprime = 1, last if 0 == $i % $p; } push @primes, $i unless $notprime; } return @primes } ## use Test::More tests => 1; is_deeply([primes_original(10000)], [primes_skipper(10000)], "same"); cmpthese(-10, { primes_original => 'primes_original (10000)', primes_skipper => 'primes_skipper (10000)', primes_opt_till_11 => 'primes_opt_till_11 (10000)', });
You will see a significant decreese of speed if you skip numbers using a regex:
1..1 ok 1 - same Rate primes_skipper primes_original primes_o +pt_till_11 primes_skipper 33.5/s -- -35% + -94% primes_original 51.4/s 53% -- + -90% primes_opt_till_11 519/s 1448% 909% + --

HtH
L*
There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re^8: Number functions.. primes (ugly code runs faster) by Discipulus
in thread Number functions I have lying around by Lady_Aleena

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.