Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re^2: is it prime? by tobyink
in thread is it prime? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (6)
    As of 2018-01-17 05:52 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      How did you see in the new year?










      Results (196 votes). Check out past polls.

      Notices?