super_pandigital_numbers.pl 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # Date: 20 August 2017
  4. # https://github.com/trizen
  5. # Generate the smallest super-pandigital numbers that are simultaneously pandigital in all bases from 2 to n inclusively.
  6. # Brute-force solution.
  7. # See also:
  8. # # https://projecteuler.net/problem=571
  9. use 5.010;
  10. use strict;
  11. use warnings;
  12. use List::Util qw(uniq all min);
  13. use ntheory qw(todigits fromdigits);
  14. use Algorithm::Combinatorics qw(variations);
  15. my $base = shift(@ARGV) // 10; # pandigital in all bases 2..$base
  16. my $first = 10; # generate first n numbers
  17. my @digits = (1, 0, 2 .. $base - 1);
  18. my @bases = reverse(2 .. $base - 1);
  19. my $sum = 0;
  20. my $iter = variations(\@digits, $base);
  21. while (defined(my $t = $iter->next)) {
  22. if ($t->[0]) {
  23. my $d = fromdigits($t, $base);
  24. if (all { uniq(todigits($d, $_)) == $_ } @bases) {
  25. say "Found: $d";
  26. $sum += $d;
  27. last if --$first == 0;
  28. }
  29. }
  30. }
  31. say "Sum: $sum";
  32. __END__
  33. First 10 super-pandigital numbers in bases 2 up to 10:
  34. 1093265784
  35. 1367508924
  36. 1432598706
  37. 1624573890
  38. 1802964753
  39. 2381059764
  40. 2409758631
  41. 2578693140
  42. 2814609357
  43. 2814759360
  44. Sum: 20319792309