068 Magic 5-gon ring.pl 881 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/perl
  2. # Author: Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Website: https://github.com/trizen
  5. # https://projecteuler.net/problem=68
  6. # Runtime: 4.326s
  7. use strict;
  8. use integer;
  9. use ntheory qw(forperm);
  10. my $max = '';
  11. my @nums = (1 .. 10);
  12. forperm {
  13. my @d = @nums[@_];
  14. my $i = $d[0] + $d[1] + $d[2];
  15. my $j = $d[3] + $d[2] + $d[4];
  16. my $k = $d[5] + $d[4] + $d[6];
  17. my $l = $d[7] + $d[6] + $d[8];
  18. my $m = $d[9] + $d[8] + $d[1];
  19. if ( $d[0] < $d[3]
  20. and $d[0] < $d[5]
  21. and $d[0] < $d[7]
  22. and $d[0] < $d[9]
  23. and $i == $j
  24. and $i == $k
  25. and $i == $l
  26. and $i == $m
  27. ) {
  28. my $str = "$d[0]$d[1]$d[2]$d[3]$d[2]$d[4]$d[5]$d[4]$d[6]$d[7]$d[6]$d[8]$d[9]$d[8]$d[1]";
  29. if (length($str) == 16 and $max lt $str) {
  30. $max = $str;
  31. }
  32. }
  33. } scalar(@nums);
  34. print "$max\n";