Matt Owen

source code for "files/pe/pe_041.pl"

return to portfolio
  1.  #!/usr/bin/perl
  2.  
  3.  use strict;
  4.  use warnings;
  5.  use combinatorics;
  6.  
  7.  
  8.  # open file
  9.  open PRIMES, "primes_under_1000000.TXT" or die("CAN'T OPEN FILE.\n");
  10.  
  11.  
  12.  # init
  13.  my %primes;
  14.  my ($n, $c);
  15.  my $p = '';
  16.  my $largest = 0;
  17.  
  18.  $primes{int $_} = 1 for (grep {$_ < sqrt 7654321 } <PRIMES>);
  19.  
  20.  
  21.  # work
  22.  for (my $k=0; $k<7*6*5*4*3*2-1; $k++) {
  23.   $n = int join '', combinatorics::permute($k, 1..7);
  24.   $c = 0;
  25.  
  26.   next unless (($n-1)%6 == 0 || ($n+1)%6 == 0 || $n%2 == 1);
  27.  
  28.   for $p (keys %primes) {
  29.   if ($n%$p == 0 && $p < $n) {
  30.   $c++;
  31.   last;
  32.   }
  33.   }
  34.  
  35.   $largest = $n if $c == 0 && $largest < $n;
  36.  }
  37.  
  38.  
  39.  # print
  40.  print $largest;