Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: is it prime?

by johngg (Abbot)
on May 06, 2012 at 12:15 UTC ( #969135=note: print w/ replies, xml ) Need Help??


in reply to is it prime?

One speedy way is to use the Sieve of Eratosthenes.

use strict; use warnings; use 5.010; say qq{$_ is }, isPrime( $_ ) ? q{} : q{not }, q{prime} for 75, 169, 229, 367, 369, 8794, 9227; sub isPrime { 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 ); }

The output.

75 is not prime 169 is not prime 229 is prime 367 is prime 369 is not prime 8794 is not prime 9227 is prime

I quickly adapted this from a much older program I had lying around that lists all the primes up to a limit.

# Script to find prime numbers up to a given limit using # the algorithm called "The Sieve of Eratosthenes." The # "sieve" is filled by marking multiples of each prime found # as "not a prime" so the first prime we find is 2 and so # all even numbers are marked. The next number along the # "sieve" that is not marked will be 3 so all multiples of # 3 are then marked, und so weiter. # use strict; use warnings; # Read number to find primes up to from command-line and # make sure it is a positive integer. Algorithm has found # and marked all non-primes up to $limit once the value # being examined exceeds the square root of $limit so # set up that value to avoid recalulation each time. # my $limit = shift or die qq{No limit supplied\n}; die qq{Limit is not a positive integer\n} unless $limit =~ /^\+?\d+$/; my $sqrtLimit = sqrt $limit; # Initialise the "sieve," for which we use a vector. The # numbers 0 and 1 are not prime so set their positions # in the vector to true. Assume that our $limit is a # prime for now by setting to false. We now have a vector # of the correct length. # my $sieve = q{}; vec( $sieve, 0, 1 ) = 1; vec( $sieve, 1, 1 ) = 1; vec( $sieve, $limit, 1 ) = 0; # In initialising the "sieve" we have reached number 1 # so iterate in a while loop until we pass $sqrtLimit. # my $reached = 1; while ( $reached < $sqrtLimit ) { # Examine the next number after $reached and keep # moving along until we find a number that hasn't # been marked as "not a prime." # my $prime = $reached + 1; ++ $prime while vec( $sieve, $prime, 1 ); # This is a prime so print it out, mark multiples # of the prime we have found as "not a prime" up # to our $limit. Update where we have reached. # print qq{$prime is a prime\n}; my $fill = 2 * $prime; while ( $fill <= $limit ) { vec( $sieve, $fill, 1 ) = 1; $fill += $prime; } $reached = $prime; } # We have passed $sqrtLimit so all primes up to $limit have # been found. It just remains to print them out. # foreach my $value ( $reached + 1 .. $limit ) { print qq{$value is a prime\n} unless vec($sieve, $value, 1); }

I hope this is of interest.

Update: Not so speedy, apparently! If only I had a computer science background and understood all the O(log n) gubbins :-D

Also updated the broken link.

Cheers,

JohnGG


Comment on Re: is it prime?
Select or Download Code
Re^2: is it prime?
by JavaFan (Canon) on May 06, 2012 at 15:12 UTC
    One speedy way is to use the Sieve of Eratosthenes.
    It's speedy if you want all primes below a certain number, or if you need to determine primality of "a lot of" numbers, but it isn't for checking a single number.

    Checking all the possible divisors can be done in O(√N) (throw in a factor of O(log n) to do the division if you have really big numbers). However, just initializing the sieve will take you Ω(N) time. And then you still have to do the work: Ω(N/pi) for the ith prime.

    The fastest way is probably just to use bigprimes.net, which has the first 1.4 billion primes on file.

Re^2: is it prime?
by tobyink (Abbot) on May 06, 2012 at 19:41 UTC

    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:

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      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

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2014-12-20 23:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (99 votes), past polls