source code for "files/pe/pe_035.pl"
return to portfolio
- #!/usr/bin/perl
- use strict;
- use warnings;
- # rotates a string
- sub rotate {
- my @s = split //, shift;
- push @s, shift(@s);
- return join '', @s;
- }
- # open file handle
- open(PRIMES, "primes_under_1000000.TXT") or die("CAN'T OPEN FILE.");
- # init
- my %primes;
- my ($n, $qprimes);
- my $count = 0;
- # make sure they're ints and have no new lines
- for (<PRIMES>) {
- chomp;
- $primes{int $_} = 1;
- }
- # brute force - can be slightly optimized by incrementing count by
- # length of the circular prime (then removing all occurences from %primes)
- for (keys %primes) {
- $n = $_;
- $qprimes = 0;
- for (1..length $n) {
- $qprimes++ if defined $primes{$n};
- $n = rotate $n;
- }
- $count++ if $qprimes == length $n;
- }
- print $count;