-
Notifications
You must be signed in to change notification settings - Fork 7
/
060 Prime pair sets.pl
69 lines (53 loc) · 1.67 KB
/
060 Prime pair sets.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
62
63
64
65
66
67
68
69
#!/usr/bin/perl
# Author: Daniel "Trizen" Șuteu
# Date: 16 August 2016
# License: GPLv3
# Website: https://github.com/trizen
# https://projecteuler.net/problem=60
# Runtime: 0.525s
use 5.010;
use strict;
use ntheory qw(primes is_prime);
my @primes = @{primes(10000)};
my $end = $#primes;
for my $i (0 .. $end-4) {
my $p1 = $primes[$i];
for my $j ($i+1 .. $end-3) {
my $p2 = $primes[$j];
( is_prime("$p1$p2")
and is_prime("$p2$p1"))
|| next;
for my $k ($j+1 .. $end-2) {
my $p3 = $primes[$k];
( is_prime("$p1$p3")
and is_prime("$p3$p1")
and is_prime("$p2$p3")
and is_prime("$p3$p2"))
|| next;
for my $l ($k+1 .. $end-1) {
my $p4 = $primes[$l];
( is_prime("$p1$p4")
and is_prime("$p4$p1")
and is_prime("$p2$p4")
and is_prime("$p4$p2")
and is_prime("$p3$p4")
and is_prime("$p4$p3"))
|| next;
for my $m ($l+1 .. $end) {
my $p5 = $primes[$m];
( is_prime("$p1$p5")
and is_prime("$p5$p1")
and is_prime("$p2$p5")
and is_prime("$p5$p2")
and is_prime("$p3$p5")
and is_prime("$p5$p3")
and is_prime("$p4$p5")
and is_prime("$p5$p4"))
|| next;
say "sum($p1, $p2, $p3, $p4, $p5) = ", $p1 + $p2 + $p3 + $p4 + $p5;
exit;
}
}
}
}
}