Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Number functions I have lying around

by Discipulus (Canon)
on Mar 31, 2015 at 09:23 UTC ( [id://1121952]=note: print w/replies, xml ) Need Help??


in reply to Re: Number functions I have lying around
in thread Number functions I have lying around

ack! ouch! choroba now i need to replace the code for primality check taken from this node used in my Tk-Tartaglia. I think your code is worth to put in the previously mentioned thread.
Rate la di ch la 0.325/s -- -99% -99% di 25.0/s 7572% -- -49% ch 48.6/s 14822% 95% --


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^3: Number functions I have lying around
by johngg (Canon) on Mar 31, 2015 at 10:48 UTC

    Eratosthenes was a clever chap!

    use strict; use warnings; use Benchmark qw{ cmpthese }; use Test::More tests => 1; is_deeply( [ primes( 10000 ) ], [ primes_jg( 10000 ) ], 'same' ); cmpthese( -10, { ch => 'primes( 10000 )', jg => 'primes_jg( 10000 )', } ); sub primes_jg { my $limit = shift; my $sqrtLimit = sqrt $limit; my $sieve = q{}; vec( $sieve, 0, 1 ) = 1; vec( $sieve, 1, 1 ) = 1; vec( $sieve, $limit, 1 ) = 0; my @primes; my $reached = 1; while( $reached < $sqrtLimit ) { my $prime = $reached + 1; ++ $prime while vec( $sieve, $prime, 1 ); push @primes, $prime; my $fill = 2 * $prime; while( $fill <= $limit ) { vec( $sieve, $fill, 1 ) = 1; $fill += $prime; } $reached = $prime; } foreach my $value ( $reached + 1 .. $limit ) { push @primes, $value unless vec( $sieve, $value, 1 ); } return @primes; } sub primes { my $n = shift; return if $n < 2; my @primes = (2); for my $i (3 .. $n) { my $sqrt = sqrt $i; my $notprime; for my $p (@primes) { last if $p > $sqrt; $notprime = 1, last if 0 == $i % $p; } push @primes, $i unless $notprime; } return @primes }
    1..1 ok 1 - same Rate ch jg ch 71.0/s -- -25% jg 94.7/s 33% --

    I hope this is of interest.

    Cheers,

    JohnGG

      "Eratosthenes was a clever chap"

      I never met him. But another chap wrote some kind of shortcut:

      use Math::Prime::XS qw( sieve_primes ); my @primes = sieve_primes(10_000);

      I'm to tired to write the benchmarks...

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

      choroba optimezed is still little faster..
      Rate la di ch jg ch_opt la 0.327/s -- -99% -99% -99% -99% di 25.3/s 7628% -- -48% -52% -54% ch 48.6/s 14733% 92% -- -7% -13% jg 52.3/s 15877% 107% 8% -- -6% ch_opt 55.6/s 16875% 120% 14% 6% --
      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.

      johngg, would you please explain what you are doing with vec. I read the doc on it and got lost in the first sentence. I don't know how you are using bit vectors (this is the first time I've ever heard of them). Everything between my $sqrtLimit = sqrt $limit; to return @primes; is a big mystery to me.

      You are also not eliminating numbers which end with 2, 4, 5, 6, 8, and 0 right off the bat, why?

      Thanks for stopping by.

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

        Lady_Aleena, the Sieve of Eratosthenes is a well-known approach to generating primes. To illustrate, suppose you want to generate the primes between 2 and 20 (as neither 0 and 1 are prime, by definition).

        Hope that helps.

        Update: 2015-04-01
        Updated formatting of example lines to highlight overstrike of values.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-26 07:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found