### Re^2: is it prime?

by tobyink (Abbot)
 on May 06, 2012 at 19:41 UTC ( #969167=note: print w/replies, xml ) Need Help??

in reply to Re: is it prime?

One speedy way is to use the Sieve of Eratosthenes.

Speedy? Benchmarking mine against yours...

```  NUMBER : TOBYINK       JOHNGG
NUMBER : RESULT  TIME  RESULT  TIME
75 : NO  0.000065  NO  0.000215
169 : NO  0.000057  NO  0.000649
229 : YES 0.000070  YES 0.000888
367 : YES 0.000073  YES 0.001437
369 : NO  0.000052  NO  0.000805
8794 : NO  0.000051  NO  0.008682
9227 : YES 0.000113  YES 0.031170
10807 : NO  0.000131  NO  0.047135
11939 : YES 0.000121  YES 0.045032
14803 : NO  0.000122  NO  0.054764
19937 : YES 0.000143  YES 0.070692
19938 : NO  0.000040  NO  0.020510
39783 : NO  0.000057  NO  0.065555
47083 : NO  0.000230  NO  0.170125
199933 : YES 0.000346  YES 0.778786

For some of those higher numbers, mine is about 2000 times faster than yours. The sieve is efficient if you need to generate a list of all prime numbers below x, but very slow as a mechanism for determining whether a given number is prime.

Benchmark script:

```use 5.010;
use strict;

use Carp qw/croak/;
use Time::HiRes qw/time/;

sub isPrime1 (_)
{
my \$num = shift;

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

croak "usage: isPrime(NATURAL NUMBER)"
unless \$num =~ /^[1-9][0-9]*\$/;

for my \$div (2 .. sqrt \$num)
{
return if \$num % \$div == 0;
}
return 1;
}

sub isPrime2
{
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);
}

say q[  NUMBER : TOBYINK       JOHNGG        ];
say q[  NUMBER : RESULT  TIME  RESULT  TIME  ];

for my \$num (qw(75 169 229 367 369 8794 9227 10807 11939 14803 19937 1
+9938 39783 47083 199933))
{
printf('%8d : ', \$num);
timeit(\$num, \$_) for \&isPrime1, \&isPrime2;
print "\n";
}

sub timeit
{
my (\$n, \$f) = @_;
my \$start = time();
print(\$f->(\$n) ? "YES " : "NO  ");
my \$end = time();
printf '%.06f  ', (\$end - \$start);
}
perl -E'sub Monkey::do{say\$_,for@_,do{(\$monkey=[caller(0)]->[3])=~s{::}{ }and\$monkey}}"Monkey say"->Monkey::do'

Replies are listed 'Best First'.
Re^3: is it prime?
by johngg (Abbot) on May 09, 2012 at 09:33 UTC

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.

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

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://969167]
help
Chatterbox?
 [choroba]: The problem was bigger, as the test tried to call a method that didn't exist anymore [marinersk]: :: ducking :: [choroba]: because, someone renamed the method, but didn't notice it was used in the test, as the test was skipped [marinersk]: Well, if the method doesn't exist, it would be hard to pass the test. [choroba]: later, someone removed the new method, as all its usage places were safe, but didn't notice the test still used the old name [choroba]: fortunately, it wasn't that hard to replace the method and fix a few remaining failures due to the changes we did to the codebase over the years [marinersk]: choroba Sounds like a process improvement opportunity; tests may not all need to be run, but they should all be compiled with perl -c before check-in/promotion happens. [choroba]: so, now I have the test, so I can start making changes in the code. Back to the original ticket, yay! [marinersk]: I definitely love it when my \$current_taask=& taskPop(); if (\$current_task eq \$original_task) { } is true. [marinersk]: s/taask/task/g;

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (14)
As of 2017-05-25 15:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
My favorite model of computation is ...

Results (187 votes). Check out past polls.