1193 is 'rotationally prime' because 1193 and all of it's rotations (1931, 9311, and 3119) are prime. There is another set of 4-digit, and two sets of 5-digit rot-prime numbers. I think that's cool, but I'm not sure why...

Update: changed stop criteria in sub primelist from test/2 to sqrt(test). Thanks to larsen.

Also, there are two sets of 6 digit rot-prime numbers.

YuckFoo

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

In reply to 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.