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

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

Replies are listed 'Best First'.
Re^4: Number functions I have lying around
by karlgoethebier (Abbot) on Mar 31, 2015 at 22:03 UTC
    "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»

Re^4: Number functions I have lying around
by Discipulus (Canon) on Mar 31, 2015 at 12:22 UTC
    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.
Re^4: Number functions I have lying around
by Lady_Aleena (Priest) on Mar 31, 2015 at 18:54 UTC

    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.