-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy path111 Primes with runs -- v2.pl
More file actions
86 lines (69 loc) · 1.53 KB
/
111 Primes with runs -- v2.pl
File metadata and controls
86 lines (69 loc) · 1.53 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 09 February 2017
# Website: https://github.com/trizen
# https://projecteuler.net/problem=111
# Runtime: 0.065s
use 5.010;
use strict;
use ntheory qw(is_prime);
my $len = 10;
my %table = (
0 => 8,
1 => 9,
2 => 8,
3 => 9,
4 => 9,
5 => 9,
6 => 9,
7 => 9,
8 => 8,
9 => 9,
);
sub comb_1 {
my ($digit) = @_;
my $r = $len - 1;
my @p = ($digit) x $r;
my $sum = 0;
foreach my $i (0 .. $r) {
foreach my $d (0 .. 9) {
my @a = @p;
splice(@a, $i, 0, $d);
my $n = join('', @a);
$sum += $n if $a[0] && is_prime($n);
}
}
return $sum;
}
sub comb_2 {
my ($digit) = @_;
my $r = $len - 2;
my @p = ($digit) x $r;
my $sum = 0;
foreach my $i (0 .. $r) {
foreach my $d1 (0 .. 9) {
foreach my $j (0 .. $r) {
foreach my $d2 (0 .. 9) {
my @a = @p;
splice(@a, $i, 0, $d1);
splice(@a, $j, 0, $d2);
my $n = join('', @a);
$sum += $n if $a[0] && is_prime($n);
}
}
}
}
return $sum;
}
my $total = 0;
foreach my $d (keys %table) {
my $max = $table{$d};
if ($max == 8) {
$total += comb_2($d);
}
elsif ($max == 9) {
$total += comb_1($d);
}
}
say $total;