-
Notifications
You must be signed in to change notification settings - Fork 7
/
060 Prime pair sets -- v2.pl
60 lines (45 loc) · 1.28 KB
/
060 Prime pair sets -- 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
55
56
57
58
59
60
#!/usr/bin/perl
# Author: Trizen
# Date: 18 March 2023
# https://github.com/trizen
# https://projecteuler.net/problem=60
# Generic solution.
# Runtime: 4.837s
use 5.036;
use ntheory qw(:all);
for (my @primes = @{primes(2)} ; ; @primes = @{primes($primes[-1] * 2)}) {
say "Checking primes <= $primes[-1]";
foreach my $i (0 .. $#primes) {
my $p = $primes[$i];
my @indx = ();
my @good = ($p);
for (my $j = $i + 1 ; ; ++$j) {
my $q = $primes[$j] // do {
@indx or last;
shift @good;
$j = shift @indx;
next;
};
my $ok = 1;
unshift @good, $q;
OUTER: for my $k (0 .. $#good) {
for my $l ($k + 1 .. $#good) {
(is_prime("$good[$k]$good[$l]") and is_prime("$good[$l]$good[$k]")) || do {
$ok = 0;
last OUTER;
}
}
}
if ($ok) {
unshift @indx, $j;
if (scalar(@good) == 5) {
say "sum(@good) = ", vecsum(@good);
exit(0);
}
}
else {
shift @good;
}
}
}
}