Some minor (possible) improvements:

sub next_prime_iterator { my $num = shift(@_); return 3 if $num < 3; do { $num += 2 } until is_prime($num); return $num; } sub is_prime { my $guess = shift(@_); my $divisor = 3; my $quotient; while(1) { $quotient= $guess / $divisor; return 1 if $quotient < $divisor; return 0 if int($quotient) == $quotient; $divisor += 2; } }
Note that using int($quotient) == $quotient made me worry that this wouldn't always work if $guess is very near the mantissa limit on your machine. But it turns out that Perl's % doesn't do any better there anyway (it does worse):
my $x= 1+~0; $x *= 2 while $x != $x+1; $x /= 2; my $y= $x; for( 1..5 ) { $x++; print $x-$y, " ", $x%3, " ", $x/3-int($x/3), "; "; } print $/ __END__ 1 0 0.75; 2 0 0; 3 2 0.25; 4 2 0.75; 5 1 0;
So the first numbers (1 2 3 4 5) show that Perl can handle those integers exactly. The second numbers (0 0 2 2 1) don't follow the pattern of 2 0 1 2 0 1 2 0 1 as they should so % is returning invalid results here. The third numbers (0.75 0 0.25 0.75 0) are following the correct pattern (something close to 0.66 0 0.33 0.66 0 0.33) so I trust my int($quotient) == $quotient test.

                - tye

In reply to Re^2: Finding the next larger prime. (improve) by tye
in thread Finding the next larger prime. by BrowserUk

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, details, 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, summary, 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.