039 Integer right triangles.pl 583 B

123456789101112131415161718192021222324252627282930313233
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 27 January 2017
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=39
  7. # Runtime: 0.056s
  8. use 5.010;
  9. use strict;
  10. use ntheory qw(is_power sqrtint);
  11. my %t;
  12. my $limit = 1000;
  13. foreach my $i (1 .. $limit / 3 - 1) {
  14. foreach my $j ($i + 1 .. ($limit - $i) / 2) {
  15. my $c = $i**2 + $j**2;
  16. last if ($i + $j + sqrt($c) > $limit);
  17. if (is_power($c, 2)) {
  18. my $k = sqrtint($c);
  19. ++$t{$i + $j + $k};
  20. }
  21. }
  22. }
  23. say +(sort { $t{$b} <=> $t{$a} } keys %t)[0];