#!/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 (default: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]; };