Thanks for spotting it. Lesson learned: never skip tests for data with more than five elements.
So for the prime number 149 the hardcoded check has to be:
if($i > 149 && $i % 149 == 0){next}
All the matter about how many primes check to hardcode and about increese and decreese of performances was vain.
But even if it was just a divertissment now i have to go on..
With the correct code, primes between 11 and 37 gives good results, especially 11.
The prime number 11 was the winner benchmarking prime reserch in numbers up to 10K, 100K e 1M.
So the answer is no more 42. 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.
| [reply] [d/l] |
Discipulus, in all of this really mind-boggling code, why aren't you eliminating the numbers which end with 2, 4, 5, 6, 8, and 0 before doing any math or other munging on the number? Wouldn't eliminating those numbers right at the start, before any fancy munging, save time? Those numbers (with the exception of '2' and '5') will never be prime, so why are you even munging them?
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] |
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:
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.
| [reply] [d/l] [select] |
| [reply] |