Well, i just wrote my own prime checker that is really a growing sieve that goes as far as it needs to for the prime to check. It caches a hash of all the primes known so far, so that primeness only needs to be calculated once, and is quite fast. I didnt even bother explicitly eliminating even numbers in advance, but that would be no biggie. The general sieve'ing can be improved some too. So here it is:
{ my %prime_cache; my $max_checked; BEGIN { $prime_cache{2} = undef; $max_checked = 2; } sub is_prime { my ($to_check) = @_; sieve_up_to($to_check); return exists $prime_cache{$to_check}; } sub sieve_up_to { my ($to_check) = @_; while ( $max_checked < $to_check ) { sieve_part($to_check); } } sub sieve_part { my ($to_check) = @_; $to_check++ if !($to_check % 2); my $max_this_part = $to_check; if ( ($max_checked * $max_checked) < $to_check ) { $max_this_part = $max_checked * $max_checked; } my %to_sieve = map { $_ => undef } ($max_checked + 1 .. $max_t +his_part); for my $prime (keys %prime_cache) { my $base = $max_checked - ($max_checked % $prime) + $prime +; while ( $base <= $max_this_part ) { delete $to_sieve{$base}; $base += $prime; } } while ( my $new_prime = each %to_sieve ) { $prime_cache{$new_prime} = undef; } $max_checked = $max_this_part; } }

In reply to Re: Rotationally Prime Numbers Revisited by shemp
in thread Rotationally Prime Numbers Revisited by Limbic~Region

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.