Skip to content

Commit 22f1c61

Browse files
committed
Merge branch 'master' of github.com:perl6/perl6-examples
2 parents aacc2a9 + 66b9eb4 commit 22f1c61

File tree

3 files changed

+306
-135
lines changed

3 files changed

+306
-135
lines changed

categories/euler/prob105-shlomif.p6

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use v6;
2+
3+
sub is_special_sum_set(@A)
4+
{
5+
my $recurse;
6+
7+
$recurse = sub ($i, $B_sum, $B_count, $C_sum, $C_count) {
8+
9+
if $i == @A
10+
{
11+
if (
12+
(!$B_count) || (!$C_count)
13+
||
14+
(
15+
($B_sum != $C_sum)
16+
&&
17+
(($B_count > $C_count) ?? ($B_sum > $C_sum) !! True)
18+
&&
19+
(($C_count > $B_count) ?? ($C_sum > $B_sum) !! True)
20+
)
21+
)
22+
{
23+
# Everything is OK.
24+
return;
25+
}
26+
else
27+
{
28+
# Incorrect.
29+
X::AdHoc.new(:payload<foo>).throw;
30+
}
31+
}
32+
33+
$recurse(
34+
$i+1, $B_sum+@A[$i], $B_count+1, $C_sum, $C_count
35+
);
36+
$recurse(
37+
$i+1, $B_sum, $B_count, $C_sum+@A[$i], $C_count+1
38+
);
39+
$recurse(
40+
$i+1, $B_sum, $B_count, $C_sum, $C_count
41+
);
42+
43+
return;
44+
};
45+
46+
my $ret = True;
47+
$recurse(0, 0, 0, 0, 0);
48+
49+
CATCH {
50+
when X::AdHoc { $ret = False; }
51+
}
52+
53+
return $ret;
54+
}
55+
56+
my $total_sum = 0;
57+
58+
for 'sets.txt'.IO.lines -> $l
59+
{
60+
say "Processing $l";
61+
my @set = $l.split(',');
62+
if (is_special_sum_set(@set))
63+
{
64+
$total_sum += ([+] @set);
65+
}
66+
}
67+
say "Total Sum = $total_sum";

categories/euler/sets.txt

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
81,88,75,42,87,84,86,65
2+
157,150,164,119,79,159,161,139,158
3+
673,465,569,603,629,592,584,300,601,599,600
4+
90,85,83,84,65,87,76,46
5+
165,168,169,190,162,85,176,167,127
6+
224,275,278,249,277,279,289,295,139
7+
354,370,362,384,359,324,360,180,350,270
8+
599,595,557,298,448,596,577,667,597,588,602
9+
175,199,137,88,187,173,168,171,174
10+
93,187,196,144,185,178,186,202,182
11+
157,155,81,158,119,176,152,167,159
12+
184,165,159,166,163,167,174,124,83
13+
1211,1212,1287,605,1208,1189,1060,1216,1243,1200,908,1210
14+
339,299,153,305,282,304,313,306,302,228
15+
94,104,63,112,80,84,93,96
16+
41,88,82,85,61,74,83,81
17+
90,67,84,83,82,97,86,41
18+
299,303,151,301,291,302,307,377,333,280
19+
55,40,48,44,25,42,41
20+
1038,1188,1255,1184,594,890,1173,1151,1186,1203,1187,1195
21+
76,132,133,144,135,99,128,154
22+
77,46,108,81,85,84,93,83
23+
624,596,391,605,529,610,607,568,604,603,453
24+
83,167,166,189,163,174,160,165,133
25+
308,281,389,292,346,303,302,304,300,173
26+
593,1151,1187,1184,890,1040,1173,1186,1195,1255,1188,1203
27+
68,46,64,33,60,58,65
28+
65,43,88,87,86,99,93,90
29+
83,78,107,48,84,87,96,85
30+
1188,1173,1256,1038,1187,1151,890,1186,1184,1203,594,1195
31+
302,324,280,296,294,160,367,298,264,299
32+
521,760,682,687,646,664,342,698,692,686,672
33+
56,95,86,97,96,89,108,120
34+
344,356,262,343,340,382,337,175,361,330
35+
47,44,42,27,41,40,37
36+
139,155,161,158,118,166,154,156,78
37+
118,157,164,158,161,79,139,150,159
38+
299,292,371,150,300,301,281,303,306,262
39+
85,77,86,84,44,88,91,67
40+
88,85,84,44,65,91,76,86
41+
138,141,127,96,136,154,135,76
42+
292,308,302,346,300,324,304,305,238,166
43+
354,342,341,257,348,343,345,321,170,301
44+
84,178,168,167,131,170,193,166,162
45+
686,701,706,673,694,687,652,343,683,606,518
46+
295,293,301,367,296,279,297,263,323,159
47+
1038,1184,593,890,1188,1173,1187,1186,1195,1150,1203,1255
48+
343,364,388,402,191,383,382,385,288,374
49+
1187,1036,1183,591,1184,1175,888,1197,1182,1219,1115,1167
50+
151,291,307,303,345,238,299,323,301,302
51+
140,151,143,138,99,69,131,137
52+
29,44,42,59,41,36,40
53+
348,329,343,344,338,315,169,359,375,271
54+
48,39,34,37,50,40,41
55+
593,445,595,558,662,602,591,297,610,580,594
56+
686,651,681,342,541,687,691,707,604,675,699
57+
180,99,189,166,194,188,144,187,199
58+
321,349,335,343,377,176,265,356,344,332
59+
1151,1255,1195,1173,1184,1186,1188,1187,1203,593,1038,891
60+
90,88,100,83,62,113,80,89
61+
308,303,238,300,151,304,324,293,346,302
62+
59,38,50,41,42,35,40
63+
352,366,174,355,344,265,343,310,338,331
64+
91,89,93,90,117,85,60,106
65+
146,186,166,175,202,92,184,183,189
66+
82,67,96,44,80,79,88,76
67+
54,50,58,66,31,61,64
68+
343,266,344,172,308,336,364,350,359,333
69+
88,49,87,82,90,98,86,115
70+
20,47,49,51,54,48,40
71+
159,79,177,158,157,152,155,167,118
72+
1219,1183,1182,1115,1035,1186,591,1197,1167,887,1184,1175
73+
611,518,693,343,704,667,686,682,677,687,725
74+
607,599,634,305,677,604,603,580,452,605,591
75+
682,686,635,675,692,730,687,342,517,658,695
76+
662,296,573,598,592,584,553,593,595,443,591
77+
180,185,186,199,187,210,93,177,149
78+
197,136,179,185,156,182,180,178,99
79+
271,298,218,279,285,282,280,238,140
80+
1187,1151,890,593,1194,1188,1184,1173,1038,1186,1255,1203
81+
169,161,177,192,130,165,84,167,168
82+
50,42,43,41,66,39,36
83+
590,669,604,579,448,599,560,299,601,597,598
84+
174,191,206,179,184,142,177,180,90
85+
298,299,297,306,164,285,374,269,329,295
86+
181,172,162,138,170,195,86,169,168
87+
1184,1197,591,1182,1186,889,1167,1219,1183,1033,1115,1175
88+
644,695,691,679,667,687,340,681,770,686,517
89+
606,524,592,576,628,593,591,584,296,444,595
90+
94,127,154,138,135,74,136,141
91+
179,168,172,178,177,89,198,186,137
92+
302,299,291,300,298,149,260,305,280,370
93+
678,517,670,686,682,768,687,648,342,692,702
94+
302,290,304,376,333,303,306,298,279,153
95+
95,102,109,54,96,75,85,97
96+
150,154,146,78,152,151,162,173,119
97+
150,143,157,152,184,112,154,151,132
98+
36,41,54,40,25,44,42
99+
37,48,34,59,39,41,40
100+
681,603,638,611,584,303,454,607,606,605,596

0 commit comments

Comments
 (0)