Matt Owen

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

return to portfolio
  1.  #!/usr/bin/perl
  2.  
  3.  use strict;
  4.  use warnings;
  5.  use euler;
  6.  
  7.  
  8.  
  9.  my $largest = 0;
  10.  my $count = 0;
  11.  
  12.  my ($solution);
  13.  
  14.  for my $p (1..1000) {
  15.   $count = 0;
  16.  
  17.   for my $a (1..$p/4) { # by triangle inequality
  18.   my $b = $p*($p-2*$a)/(2*($p-$a)); # by solving system of equations
  19.   my $c = sqrt($a**2+$b**2); # by pythagorean theorem
  20.   $count++ if $c == int $c; # because we want integer solutions only
  21.   }
  22.  
  23.   if ($largest < $count) {
  24.   $largest = $count;
  25.   $solution = $p;
  26.   }
  27.  }
  28.  
  29.  
  30.  print $largest, ' ', $solution;