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
| [reply] |
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,
| [reply] [d/l] [select] |
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 :-)
| [reply] [d/l] [select] |