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

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

by choroba (Cardinal)
on Apr 02, 2015 at 09:18 UTC ( [id://1122238]=note: print w/replies, xml ) Need Help??


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

Trying to divide by the known primes is what the algorithm does. Hardcoding some of the values might speed it up, but notice that your solution fails the tests - it doesn't return all the hardcoded primes.
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
  • Comment on Re^5: Number functions.. primes (ugly code runs faster)

Replies are listed 'Best First'.
Re^6: Number functions.. primes (ugly code runs faster)
by Discipulus (Canon) on Apr 02, 2015 at 20:55 UTC
    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.

      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.

Log In?
Username:
Password:

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

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

    No recent polls found