Neat algorithm. I'll arrogantly rewrite your code to make it what I consider to be more Perlish. :-)

This takes advantage of a number of things some C/C++ programmers may or may not know/be comfortable with.

#!/usr/local/bin/perl use strict; use warnings; use constant DIGITS => (5); my $list = primelist(10 ** DIGITS); $list = rotatelist($list); print $_, $/ for @$list; #----------------------------------------------------------- sub rotatelist { my ($primes) = @_; my %phash; @phash{@{$primes}} = (1) x @{$primes}; my @list; for my $prime (@{$primes}) { my $rotations = rotations($prime); if ($prime == $rotations->[0]) { my $sum = 0; $sum += $phash{$_} || 0 for @$rotations; push @list, $prime if $sum == @{$rotations}; } } return \@list; } #----------------------------------------------------------- sub rotations { my ($num) = @_; my @digs = split '', $num; my @list; for (1 .. length($num)) { push @digs, shift @digs; push @list, join ('', @digs) + 0; } @list = sort(@list); return \@list; } #----------------------------------------------------------- sub primelist { my ($max) = @_; my @primes = (2); for (my $test = 3; $test < $max; $test += 2) { my $stop = sqrt($test); my $isprime = 1; for my $prime (@primes) { last if $prime >= $stop; if ($test % $prime == 0) { $isprime = 0; last; } } push @primes, $test if $isprime; } return \@primes; }

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: Rotationally Prime Numbers by dragonchild
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!
  • 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.