Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^6: Number functions.. primes (ugly code runs faster)

by Discipulus (Canon)
on Apr 02, 2015 at 20:55 UTC ( [id://1122306]=note: print w/replies, xml ) Need Help??


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

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.

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

    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
      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.

        Here are some of the problems.

        • Some made assumptions I knew the regex was slowing down my code. I had no idea the regex was the problem.
        • Some made assumptions of my prowess in math. If it isn't addition, subtraction, multiplication, or division; I won't think to use it. So, using sqrt is confusing.
        • Only aaron_baugher commented directly on my code and showed me where I overdid it with the check for divisibility by 9. Everyone else wrote their own versions without commenting on the code I provided first.
        • No one commented their code! I'm still stuck back at choroba's first prime example at the square root test. Adding the use of square roots still bothers me for some reason.

        Like with many car shows my hubby watches where the show glosses over a lot, it is like you took the logo off the car, trashed the car, and put the logo on another car. I still don't know what is so wrong with the original car (function) that the only thing worth saving was the logo (function name). So take one part out at a time, explain why it is broken, then install the new part telling the audience why the new part is better. Pretty please?

        No matter how hysterical I get, my problems are not time sensitive. So, relax, have a cookie, and a very nice day!
        Lady Aleena

        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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1122306]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 08:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found