-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProb35.pl
More file actions
44 lines (44 loc) · 816 Bytes
/
Prob35.pl
File metadata and controls
44 lines (44 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#! usr/bin/perl
#Matthew Digman
use strict;
my @nums=(2..1000);
my @primes;
while(@nums){
my $prime=shift @nums;
if(defined $prime){
push @primes, $prime;
for(my $itr=$prime-1; $itr<@nums;$itr+=$prime){
if(defined $nums[$itr]){
$nums[$itr]=undef;
}
}
}
}
my %primes;
$primes{$_}=1 for(@primes);
my %rotational;
for(@primes){
unless(exists $rotational{$_}){
rotate($_);
}
}
my @keys =sort{$a<=>$b} keys %rotational;
print "$#keys in @keys\n";
sub rotate{
my $num=shift;
my @rotations=($num);
for(1..(length $num)-1){
my $digit=substr($num,0,1);
my $rest=substr($num,1,(length $num)-1);
$rest.=$digit;
if(exists $primes{$rest}){
$num=$rest;
push @rotations, $num;
}else{
return 0;
}
}
for(@rotations){
$rotational{$_}=1;
}
}