-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path571 Super Pandigital Numbers -- v2.pl
More file actions
62 lines (47 loc) · 1.25 KB
/
571 Super Pandigital Numbers -- v2.pl
File metadata and controls
62 lines (47 loc) · 1.25 KB
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
62
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 20 August 2017
# https://github.com/trizen
# https://projecteuler.net/problem=571
# Brute-force solution, with some optimizations.
# Runtime: ~12 minutes.
use 5.010;
use strict;
use warnings;
use List::Util qw(all);
use ntheory qw(fromdigits);
use experimental qw(signatures);
use Algorithm::Combinatorics qw(variations);
my $base = shift(@ARGV) // 12; # 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);
sub is_8_pandigital ($n) {
my $found = 0;
while ($n > 0) {
$found |= 1 << ($n & 7);
$n >>= 3;
}
$found == (1 << 8) - 1;
}
sub is_pandigital ($n, $base) {
my $found = 0;
while ($n > 0) {
$found |= 1 << ($n % $base);
$n = int($n / $base);
}
$found == (1 << $base) - 1;
}
while (defined(my $t = $iter->next)) {
if ($t->[0]) {
my $v = fromdigits($t, $base);
if (is_8_pandigital($v) and all { is_pandigital($v, $_) } @bases) {
say "Found: $v";
$sum += $v;
last if --$first == 0;
}
}
}
say "Sum: $sum";