We don't bite newbies here... much PerlMonks

### Re^3: is it prime?

by johngg (Abbot)
 on May 09, 2012 at 09:33 UTC ( #969585=note: print w/replies, xml ) Need Help??

in reply to Re^2: is it prime?

I'd only ever used Eratosthenes' Sieve to find a list of all primes up to a limit and, stupidly, assumed that it would be equally good at testing whether a single number was prime. Silly me!

You have shown your method to be much faster but there's still room for improvement. You reject all even numbers the first time through your loop so there's no point in using even numbers again as divisor. Instead, take the evens test out of the loop then employ a C-style loop to test only odd divisors.

The code. I've tweaked things to return 0 if not prime in order to make testing easier.

```use strict;
use warnings;

use 5.010;
use Benchmark qw{ cmpthese };
use Test::More qw{ no_plan };

my %methods = (
eratosthenes => sub
{
my \$toTest    = shift;
my \$sqrtLimit = sqrt \$toTest;

my \$sieve = q{};
vec( \$sieve, 0      , 1 ) = 1;
vec( \$sieve, 1      , 1 ) = 1;
vec( \$sieve, \$toTest, 1 ) = 0;

my \$marker = 1;
while ( \$marker < \$sqrtLimit )
{
my \$possPrime = \$marker + 1;
\$possPrime ++ while vec( \$sieve, \$possPrime, 1 );

my \$fill = 2 * \$possPrime;
while ( \$fill <= \$toTest )
{
vec( \$sieve, \$fill, 1 ) = 1;
\$fill += \$possPrime;
}
last if vec( \$sieve, \$toTest, 1 );

\$marker = \$possPrime;
}

return not vec( \$sieve, \$toTest, 1 );
},
tobyink      => sub
{
my \$num = shift;

return 0 if \$num == 1;  # 1 is not prime

for my \$div (2 .. sqrt \$num)
{
return 0 if \$num % \$div == 0;
}
return 1;
},
tobyinkPlus  => sub
{
my \$toTest = shift;

return 0 if     \$toTest == 1;
return 1 if     \$toTest == 2;
return 0 unless \$toTest %  2;

my \$sqrtLimit = sqrt \$toTest;
for ( my \$div = 3; \$div <= \$sqrtLimit; \$div += 2 )
{
return 0 unless \$toTest % \$div;
}
return 1;
},
);

say qq{Testing with value 2 which is a prime};
foreach my \$method ( sort keys %methods )
{
ok(
\$methods{ \$method }->( 2 ) == 1,
qq{\$method}
);
}

say qq{Testing with value 8357 which is not a prime};
foreach my \$method ( sort keys %methods )
{
ok(
\$methods{ \$method }->( 8357 ) == 0,
qq{\$method}
);
}

say qq{Testing with value 9293 which is a prime};
foreach my \$method ( sort keys %methods )
{
ok(
\$methods{ \$method }->( 9293 ) == 1,
qq{\$method}
);
}

my @testValues;
push @testValues, int rand 1e6 for 1 .. 20;

cmpthese(
-20,
{
map
{
my \$codeStr =
q[sub { my \$isPrime = \$methods{ ] .
\$_ .
q[ }->( \$_ ) for @testValues; }];
\$_ => eval \$codeStr;
}
keys %methods
}
);

The benchmark results.

```Testing with value 2 which is a prime
ok 1 - eratosthenes
ok 2 - tobyink
ok 3 - tobyinkPlus
Testing with value 8357 which is not a prime
ok 4 - eratosthenes
ok 5 - tobyink
ok 6 - tobyinkPlus
Testing with value 9293 which is a prime
ok 7 - eratosthenes
ok 8 - tobyink
ok 9 - tobyinkPlus
Rate eratosthenes      tobyink  tobyinkPlus
eratosthenes 0.154/s           --        -100%        -100%
tobyink       1291/s      838266%           --         -35%
tobyinkPlus   1984/s     1288047%          54%           --
1..9

I hope this is of interest.

Cheers,

JohnGG

Replies are listed 'Best First'.
Re^4: is it prime?
by danaj (Friar) on Jun 09, 2014 at 08:57 UTC
Necroposting, but maybe of interest still.

First, no surprise, there are modules that will be much faster than doing it yourself, and easier overall. But let's take a look at pure Perl methods. We can improve the SoE method about 2-3x by using the simple vector sieve from RosettaCode. The sieve using a string is faster yet, but let's keep it simple since clearly this is not the right way to go about testing primality. I also included my version of the all, mod-2 wheel, and mod-6 wheel trial division methods. Although going to a mod-30 wheel can improve things a little more and still be reasonable code, I didn't include it. The mod-6 wheel runs about 2x faster than Toby's original code.

For modules, some choices include:

• Math::Prime::Util        scads faster than anything else, especially for large inputs. Needs Math::Prime::Util::GMP to be very fast for bigints, though it works fine without. I'm biased, being the author.
• Math::Prime::XS           trial division with a mod-30 wheel in XS. Very fast for small numbers, slows down rapidly, no bigint support.
• Math::Pari                  reasonably fast, supports bigints. Since it is based on the ancient Pari 2.1, be careful as it will sometimes give you incorrect results.
• Math::Primality           works, but quite slow as it is designed for bigints
• Math::Prime::FastSieve  if your range is small (e.g. under 1000M) and you have lots of numbers to test, this can work well by doing an efficient sieve in XS then do fast bit tests to determine primality.
For generation, both Math::Prime::Util and Math::Prime::FastSieve should be much faster than trying to download a list from the net, as a previous suggestion mentioned, and probably faster than loading from disk as well.

Code:

Timing results:

```Validating tests on small primes
Validating tests on small composites
TOBYINK     DIV2        DIV6        MPU
NUMBER   :  RES TIME    RES TIME    RES TIME    RES  TIME
75 :  N 0.000004  N 0.000002  N 0.000001  N 0.000002
169 :  N 0.000002  N 0.000002  N 0.000001  N 0.000000
229 :  Y 0.000004  Y 0.000001  Y 0.000001  Y 0.000000
367 :  Y 0.000004  Y 0.000002  Y 0.000002  Y 0.000000
369 :  N 0.000002  N 0.000001  N 0.000001  N 0.000000
8794 :  N 0.000002  N 0.000001  N 0.000001  N 0.000000
9227 :  Y 0.000008  Y 0.000005  Y 0.000004  Y 0.000000
10807 :  N 0.000008  N 0.000005  N 0.000004  N 0.000000
11939 :  Y 0.000009  Y 0.000005  Y 0.000005  Y 0.000000
14803 :  N 0.000009  N 0.000005  N 0.000004  N 0.000000
19937 :  Y 0.000011  Y 0.000006  Y 0.000005  Y 0.000001
19938 :  N 0.000002  N 0.000001  N 0.000001  N 0.000001
39783 :  N 0.000002  N 0.000001  N 0.000001  N 0.000000
47083 :  N 0.000014  N 0.000008  N 0.000006  N 0.000000
199933 :  Y 0.000030  Y 0.000017  Y 0.000014  Y 0.000008
75640351 :  Y 0.000554  Y 0.000311  Y 0.000246  Y 0.000001
760149189769 :  Y 0.056347  Y 0.031018  Y 0.024714  Y 0.000011
635921898906263 :  Y 1.605829  Y 0.904163  Y 0.721130  Y 0.000004

Create A New User
Node Status?
node history
Node Type: note [id://969585]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2018-01-24 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (258 votes). Check out past polls.

Notices?