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