-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy path051 Prime digit replacements -- v2.pl
54 lines (42 loc) · 1.25 KB
/
051 Prime digit replacements -- v2.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
#!/usr/bin/perl
# Author: Trizen
# Date: 18 March 2023
# https://github.com/trizen
# Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family.
# https://projecteuler.net/problem=51
# Runtime: 2.179s
use 5.036;
use ntheory qw(:all);
use List::Util qw(uniq);
foreach my $n (1 .. 15) {
my %table;
foreach my $p (@{primes(10**($n - 1), 10**$n - 1)}) {
my @s = split(//, $p);
my %indices;
foreach my $i (0 .. $#s) {
push @{$indices{$s[$i]}}, $i;
}
foreach my $idx (values %indices) {
foreach my $i (1 .. scalar(@$idx)) {
forcomb {
my $key = $p;
foreach my $i (@{$idx}[@_]) {
substr($key, $i, 1, '*');
}
push @{$table{$key}}, $p;
} scalar(@$idx), $i;
}
}
}
my $min = +'inf';
foreach my $value (values %table) {
if (scalar(@$value) == 8 and $value->[0] < $min) {
say "Found prime group: [@{$value}]";
$min = $value->[0];
}
}
if ($min != +'inf') {
say $min;
last;
}
}