Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
i wrote an algorithm to save you many, many cycles.

createPrimeVec() creates a bit vector of length $max. the value is 0 if prime, 1 otherwise. it uses the sieve of erathosthenes method to create the list. merlyn wrote a column on this some time ago (suprise!)

isprime() is a simple bit vector lookup, very fast.

rotPrime() cleverly appends the return value of chop to the beginning of the string.

here it is:

#!/usr/local/bin/perl -w use strict; $|=1; my $vecPrime; my %rotPrime; # allows specification of a range, such as (1000, 9999) my ($min, $max) = (2, 999_999); print "---creating prime vector (length: $max)\n"; $vecPrime = createPrimeVec($max); print "---created\n\n"; print "---trying rotations $min..$max\n"; for($min..$max) { next unless isprime($_); my $x = rotPrime($_); $x && $rotPrime{$x}++; } print "---tried $min..$max\n\n"; print "$_\n" for(sort {$a <=> $b} keys %rotPrime); #---------- # return list of prime numbers up to and including value passed (defau +lt:1000) sub createPrimeVec { my $UPPER = shift || 1000; my $sieve = ""; GUESS: for (my $guess = 2; $guess <= $UPPER; $guess++) { next GUESS if vec($sieve,$guess,1); for(my $mults = $guess * $guess; $mults <= $UPPER; $mults += $ +guess) { vec($sieve,$mults,1) = 1; } } return $sieve; }; # returns true if found in prime number bit vector sub isprime { return 1 if( vec($vecPrime,shift,1) == 0 ); }; # return smallest 'rotationally prime' number in set # for instance, 1931, 9311, 3119, or 1193 yeilds return value 1193 sub rotPrime { my ($num, $lchr, @list) = (shift); for(1..length $num) { $lchr = chop $num; $num = $lchr . $num; push @list, $num; return unless isprime($num); } return (sort @list)[0]; };
i had used the prime number code for a program using fermat's factoring algorithm. perhaps i'll post it, assuming there's not a module to do that already.

~Particle


In reply to Re: Rotationally Prime Numbers by particle
in thread Rotationally Prime Numbers by YuckFoo

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chilling in the Monastery: (7)
    As of 2014-09-20 09:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (157 votes), past polls