Project Euler Problem 38

//

I wrote a bunch of these but never posted them up, so I'm just uploading them in bulk now. Not much to say about them at this point.

  1. #!/usr/bin/perl -w
  2.  
  3. sub is_pandigital {
  4.     my $n = shift;
  5.     my $seen;
  6.     my @digits = split(//, $n);
  7.     foreach my $d (@digits) {
  8.         return 0 if($seen->{$d});
  9.         return 0 if($d eq "0");
  10.         $seen->{$d} = 1;
  11.     }
  12.     return 1;
  13. }
  14.  
  15. sub p38 {
  16.     my $len = 0;
  17.     my $max = 0;
  18.     for(my $k = 100000; $k > 0; $k--) {
  19.         my $prod    ='';
  20.         my $n       = 1;
  21.         while(length($prod) <= 9) {
  22.             my $p = $n*$k;
  23.             $prod .= "$p";
  24.             if(length($prod) == 9) {
  25.                 if(is_pandigital($prod)) {
  26.                     if($prod > $max) {
  27.                         $max = $prod;
  28.                         warn "new max: ($k, $n, $max)";
  29.                     }
  30.                 }
  31.             }
  32.             $n++;
  33.         }
  34.     }
  35.     print "$max\n";
  36. }
  37. p38();