summaryrefslogtreecommitdiffstats
path: root/041.pl
blob: b53825b89aa57454952258dbd2cc9abf6d19f775 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/perl
use strict;
use warnings;
use Math::Prime::XS qw/sieve_primes/;
use Math::Combinatorics;

my @primes = sieve_primes(8e6);
my %primes = map { $_, 1 } @primes;

my $max = 0;
# can skip 8 and 9 because 36 and 45 are divisible by 3
for my $n (reverse 2..7) {
    my $comb = Math::Combinatorics->new(data => [1..$n]);
    while (my $perm = join '', $comb->next_permutation) {
        $max = $perm if $perm > $max && exists $primes{$perm};
    }
    last if $max;
}
print "$max\n";