049 Prime permutations.pl 721 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 28 January 2017
  5. # https://github.com/trizen
  6. # https://projecteuler.net/problem=49
  7. # Runtime: 0.024s
  8. use 5.010;
  9. use strict;
  10. use integer;
  11. use ntheory qw(forprimes);
  12. use List::Util qw(uniq);
  13. my %perms;
  14. forprimes {
  15. push @{$perms{join '', sort split //}}, $_;
  16. } 1000, 9999;
  17. foreach my $perm (values %perms) {
  18. (my @p = @$perm) >= 3 or next;
  19. my %diffs;
  20. foreach my $i (0 .. $#p) {
  21. foreach my $j ($i + 1 .. $#p) {
  22. push @{$diffs{$p[$j] - $p[$i]}}, $p[$i], $p[$j];
  23. }
  24. }
  25. foreach my $diff (keys %diffs) {
  26. if ((my @d = uniq(@{$diffs{$diff}})) == 3) {
  27. say "$diff: [@d]";
  28. }
  29. }
  30. }