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

I didn't understand choroba's reason for doing math on numbers which need no math to prove they are not prime.

No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
Lady Aleena
  • Comment on Re^9: Number functions.. primes (ugly code runs faster)

Replies are listed 'Best First'.
Re^10: Number functions.. primes (ugly code runs faster)
by Athanasius (Archbishop) on Apr 07, 2015 at 07:25 UTC

    Hello Lady_Aleena,

    You are correct, and by eliminating even numbers up front, along with multiples of five, choroba’s solution can be speeded up by around a third:

    But note that choroba’s solution is well over a hundred times faster than the non-sieve method you posted, so maybe an additional speedup of around 35% was irrelevant to the point he was making. :-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^10: Number functions.. primes (ugly code runs faster)
by soonix (Chancellor) on Apr 07, 2015 at 07:25 UTC
    I think the reason is in your own words :-)
    doing math on numbers
    The Perl compiler probably knows that $number % 2 is just "testing the last bit", which probably is more efficient than even the most optimized regex for testing "even"-ness. Plus you don't need to add 2 back in.

    I assume, because $number is populated by counting, Perl doesn't even need to convert from string to number, so the other special cases are possibly also not an optimization.

    For a human Perl Interpreter, of course, the situation looks different :-)