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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Rotationally Prime Numbers
by particle (Vicar) on Jan 29, 2002 at 04:32 UTC | |
by YuckFoo (Abbot) on Jan 31, 2002 at 16:11 UTC | |
by thinker (Parson) on Mar 25, 2005 at 08:39 UTC | |
Re: Rotationally Prime Numbers
by dragonchild (Archbishop) on Jan 29, 2002 at 03:14 UTC | |
Re (tilly) 1: Rotationally Prime Numbers
by tilly (Archbishop) on Feb 01, 2002 at 04:00 UTC | |
by Limbic~Region (Chancellor) on Mar 24, 2005 at 20:17 UTC | |
Re: Rotationally Prime Numbers
by Anonymous Monk on Jan 31, 2002 at 19:36 UTC | |
Re: Rotationally Prime Numbers
by trammell (Priest) on Feb 01, 2002 at 00:11 UTC |
Back to
Cool Uses for Perl