Done puzzle 64
[project-euler.git] / euler60.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "# Approach 1: generate and test\n",
8 "My first thought was to generate all tuples of five primes, up to some limit, and then test to see which of them had the concatenation property. That proved to be rather slow.\n",
9 "\n",
10 "# Approach 2: interleave generation and testing\n",
11 "The second approach was to interleave generation and testing. I maintain a list of groups that have the concatenation property, and try adding the next prime to each of the groups. Those that succeed are added to the list. Groups are never removed from the list, as a later prime may give a valid extension of the group."
12 ]
13 },
14 {
15 "cell_type": "code",
16 "execution_count": 23,
17 "metadata": {},
18 "outputs": [
19 {
20 "data": {
21 "text/plain": [
22 "1229"
23 ]
24 },
25 "execution_count": 23,
26 "metadata": {},
27 "output_type": "execute_result"
28 }
29 ],
30 "source": [
31 "require_relative 'primes'\n",
32 "require_relative 'array-numbers'\n",
33 "$primes = Primes.instance\n",
34 "$primes.primes.length"
35 ]
36 },
37 {
38 "cell_type": "code",
39 "execution_count": 3,
40 "metadata": {},
41 "outputs": [
42 {
43 "data": {
44 "text/plain": [
45 "1223"
46 ]
47 },
48 "execution_count": 3,
49 "metadata": {},
50 "output_type": "execute_result"
51 }
52 ],
53 "source": [
54 "candidates = $primes.take(200)\n",
55 "candidates.last"
56 ]
57 },
58 {
59 "cell_type": "code",
60 "execution_count": 4,
61 "metadata": {},
62 "outputs": [
63 {
64 "data": {
65 "text/plain": [
66 "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]"
67 ]
68 },
69 "execution_count": 4,
70 "metadata": {},
71 "output_type": "execute_result"
72 }
73 ],
74 "source": [
75 "ds = (0..10).to_a"
76 ]
77 },
78 {
79 "cell_type": "code",
80 "execution_count": 5,
81 "metadata": {},
82 "outputs": [
83 {
84 "data": {
85 "text/plain": [
86 ":tuples"
87 ]
88 },
89 "execution_count": 5,
90 "metadata": {},
91 "output_type": "execute_result"
92 }
93 ],
94 "source": [
95 "def tuples(items, count)\n",
96 " if count == 0\n",
97 " [[]]\n",
98 " elsif items.empty?\n",
99 " []\n",
100 " else\n",
101 " tuples(items.drop(1), count) + tuples(items.drop(1), count-1).map {|t| [items.first] + t}\n",
102 " end\n",
103 "end"
104 ]
105 },
106 {
107 "cell_type": "code",
108 "execution_count": 23,
109 "metadata": {},
110 "outputs": [
111 {
112 "data": {
113 "text/plain": [
114 "19900"
115 ]
116 },
117 "execution_count": 23,
118 "metadata": {},
119 "output_type": "execute_result"
120 }
121 ],
122 "source": [
123 "tuples(candidates, 2).length"
124 ]
125 },
126 {
127 "cell_type": "code",
128 "execution_count": 32,
129 "metadata": {},
130 "outputs": [
131 {
132 "data": {
133 "text/plain": [
134 "[[1201, 1213, 1217, 1223], [1193, 1213, 1217, 1223], [1193, 1201, 1217, 1223], [1193, 1201, 1213, 1223], [1193, 1201, 1213, 1217], [1187, 1213, 1217, 1223], [1187, 1201, 1217, 1223], [1187, 1201, 1213, 1223], [1187, 1201, 1213, 1217], [1187, 1193, 1217, 1223], [1187, 1193, 1213, 1223], [1187, 1193, 1213, 1217], [1187, 1193, 1201, 1223], [1187, 1193, 1201, 1217], [1187, 1193, 1201, 1213], [1181, 1213, 1217, 1223], [1181, 1201, 1217, 1223], [1181, 1201, 1213, 1223], [1181, 1201, 1213, 1217], [1181, 1193, 1217, 1223], [1181, 1193, 1213, 1223]]"
135 ]
136 },
137 "execution_count": 32,
138 "metadata": {},
139 "output_type": "execute_result"
140 }
141 ],
142 "source": [
143 "groups = tuples(candidates, 4)\n",
144 "groups[0..20]"
145 ]
146 },
147 {
148 "cell_type": "code",
149 "execution_count": 26,
150 "metadata": {},
151 "outputs": [
152 {
153 "data": {
154 "text/plain": [
155 "[78, 79, 710, 87, 89, 810, 97, 98, 910, 107, 108, 109]"
156 ]
157 },
158 "execution_count": 26,
159 "metadata": {},
160 "output_type": "execute_result"
161 }
162 ],
163 "source": [
164 "[7, 8, 9, 10].permutation(2).map {|a, b| (a.to_digits + b.to_digits).to_i}"
165 ]
166 },
167 {
168 "cell_type": "code",
169 "execution_count": 6,
170 "metadata": {},
171 "outputs": [
172 {
173 "data": {
174 "text/plain": [
175 ":valid_group"
176 ]
177 },
178 "execution_count": 6,
179 "metadata": {},
180 "output_type": "execute_result"
181 }
182 ],
183 "source": [
184 "def valid_group(group)\n",
185 " group.permutation(2).map {|a, b| (a.to_digits + b.to_digits).to_i}.all? {|n| $primes.is_prime? n}\n",
186 "end"
187 ]
188 },
189 {
190 "cell_type": "code",
191 "execution_count": 7,
192 "metadata": {},
193 "outputs": [
194 {
195 "data": {
196 "text/plain": [
197 ":valid_group2"
198 ]
199 },
200 "execution_count": 7,
201 "metadata": {},
202 "output_type": "execute_result"
203 }
204 ],
205 "source": [
206 "# No need to restest primality of combinations of numbers already in the group\n",
207 "def valid_group2(group, item)\n",
208 " item_digits = item.to_digits\n",
209 " group.flat_map {|a| [(a.to_digits + item_digits).to_i, \n",
210 " (item_digits + a.to_digits).to_i]}.all? {|n| $primes.is_prime? n}\n",
211 "end"
212 ]
213 },
214 {
215 "cell_type": "code",
216 "execution_count": null,
217 "metadata": {},
218 "outputs": [],
219 "source": [
220 "# groups.select {|g| valid_group g}"
221 ]
222 },
223 {
224 "cell_type": "code",
225 "execution_count": 9,
226 "metadata": {},
227 "outputs": [
228 {
229 "data": {
230 "text/plain": [
231 ":grow"
232 ]
233 },
234 "execution_count": 9,
235 "metadata": {},
236 "output_type": "execute_result"
237 }
238 ],
239 "source": [
240 "def grow(groups, item)\n",
241 " new_groups = groups.map {|g| g + [item]}.select {|g| valid_group g}\n",
242 " groups + [[item]] + new_groups\n",
243 "end"
244 ]
245 },
246 {
247 "cell_type": "code",
248 "execution_count": 10,
249 "metadata": {},
250 "outputs": [
251 {
252 "data": {
253 "text/plain": [
254 ":grow2"
255 ]
256 },
257 "execution_count": 10,
258 "metadata": {},
259 "output_type": "execute_result"
260 }
261 ],
262 "source": [
263 "def grow2(groups, item)\n",
264 " new_groups = groups.select {|g| valid_group2 g, item}.map {|g| g + [item]}\n",
265 " groups + [[item]] + new_groups\n",
266 "end"
267 ]
268 },
269 {
270 "cell_type": "code",
271 "execution_count": 11,
272 "metadata": {},
273 "outputs": [
274 {
275 "data": {
276 "text/plain": [
277 "[[2], [3], [5], [7], [3, 7], [11], [3, 11], [13], [17], [3, 17], [19], [7, 19], [13, 19], [23], [11, 23], [29], [31], [3, 31], [19, 31], [37], [3, 37], [41], [43], [47], [23, 47], [53], [59], [3, 59], [61], [7, 61], [13, 61], [67], [3, 67], [37, 67], [3, 37, 67], [71], [29, 71]]"
278 ]
279 },
280 "execution_count": 11,
281 "metadata": {},
282 "output_type": "execute_result"
283 }
284 ],
285 "source": [
286 "pgroups = []\n",
287 "$primes.take(20).each do |p|\n",
288 " pgroups = grow pgroups, p\n",
289 "end\n",
290 "pgroups"
291 ]
292 },
293 {
294 "cell_type": "code",
295 "execution_count": 12,
296 "metadata": {},
297 "outputs": [
298 {
299 "data": {
300 "text/plain": [
301 ":grow_to_size"
302 ]
303 },
304 "execution_count": 12,
305 "metadata": {},
306 "output_type": "execute_result"
307 }
308 ],
309 "source": [
310 "def grow_to_size(group_size = 5, debug = false)\n",
311 " pgroups = []\n",
312 " i = 0\n",
313 " while !pgroups.any? {|g| g.length >= group_size}\n",
314 " pgroups = grow pgroups, $primes[i]\n",
315 " puts \"#{pgroups.length} groups, added #{$primes[i]}\" if debug && (i % 50 == 0)\n",
316 " i += 1\n",
317 " end\n",
318 " \n",
319 " pgroups.select {|g| g.length >= group_size}\n",
320 "end"
321 ]
322 },
323 {
324 "cell_type": "code",
325 "execution_count": 13,
326 "metadata": {},
327 "outputs": [
328 {
329 "data": {
330 "text/plain": [
331 ":grow_to_size2"
332 ]
333 },
334 "execution_count": 13,
335 "metadata": {},
336 "output_type": "execute_result"
337 }
338 ],
339 "source": [
340 "def grow_to_size2(group_size = 5, debug = false)\n",
341 " pgroups = []\n",
342 " i = 0\n",
343 " while !pgroups.any? {|g| g.length >= group_size}\n",
344 " pgroups = grow2 pgroups, $primes[i]\n",
345 " puts \"#{pgroups.length} groups, added #{$primes[i]}\" if debug && (i % 50 == 0)\n",
346 " i += 1\n",
347 " end\n",
348 " \n",
349 " pgroups.select {|g| g.length >= group_size}\n",
350 "end"
351 ]
352 },
353 {
354 "cell_type": "code",
355 "execution_count": 25,
356 "metadata": {},
357 "outputs": [
358 {
359 "name": "stdout",
360 "output_type": "stream",
361 "text": [
362 "1 groups, added 2\n",
363 "142 groups, added 233\n",
364 "437 groups, added 547\n",
365 "Found [[3, 7, 109, 673]] in 2.166974642\n"
366 ]
367 }
368 ],
369 "source": [
370 "start = Time.now\n",
371 "found = grow_to_size 4, debug=true\n",
372 "finish = Time.now\n",
373 "puts \"Found #{found} in #{finish - start}\""
374 ]
375 },
376 {
377 "cell_type": "code",
378 "execution_count": 26,
379 "metadata": {},
380 "outputs": [
381 {
382 "name": "stdout",
383 "output_type": "stream",
384 "text": [
385 "1 groups, added 2\n",
386 "142 groups, added 233\n",
387 "437 groups, added 547\n",
388 "Found [[3, 7, 109, 673]] in 1.012126927\n"
389 ]
390 }
391 ],
392 "source": [
393 "start = Time.now\n",
394 "found = grow_to_size2 4, debug=true\n",
395 "finish = Time.now\n",
396 "puts \"Found #{found} in #{finish - start}\""
397 ]
398 },
399 {
400 "cell_type": "code",
401 "execution_count": 15,
402 "metadata": {},
403 "outputs": [
404 {
405 "name": "stdout",
406 "output_type": "stream",
407 "text": [
408 "1 groups, added 2\n",
409 "142 groups, added 233\n",
410 "437 groups, added 547\n"
411 ]
412 },
413 {
414 "data": {
415 "text/plain": [
416 "[[3, 7, 109, 673]]"
417 ]
418 },
419 "execution_count": 15,
420 "metadata": {},
421 "output_type": "execute_result"
422 }
423 ],
424 "source": [
425 "grow_to_size2 4, debug=true"
426 ]
427 },
428 {
429 "cell_type": "code",
430 "execution_count": 33,
431 "metadata": {},
432 "outputs": [
433 {
434 "name": "stdout",
435 "output_type": "stream",
436 "text": [
437 "1 groups, added 2\n",
438 "142 groups, added 233\n",
439 "437 groups, added 547\n",
440 "903 groups, added 877\n",
441 "1420 groups, added 1229\n",
442 "1926 groups, added 1597\n",
443 "2531 groups, added 1993\n",
444 "3201 groups, added 2371\n",
445 "3958 groups, added 2749\n",
446 "4678 groups, added 3187\n",
447 "5565 groups, added 3581\n",
448 "6455 groups, added 4001\n",
449 "7509 groups, added 4421\n",
450 "8594 groups, added 4861\n",
451 "9887 groups, added 5281\n",
452 "11260 groups, added 5701\n",
453 "12844 groups, added 6143\n",
454 "14344 groups, added 6577\n",
455 "16018 groups, added 7001\n",
456 "17704 groups, added 7507\n",
457 "19570 groups, added 7927\n",
458 "21743 groups, added 8389\n",
459 "Found [[13, 5197, 5701, 6733, 8389]], sum [26033], in 1095.055247849\n"
460 ]
461 }
462 ],
463 "source": [
464 "start = Time.now\n",
465 "found = grow_to_size 5, debug=true\n",
466 "finish = Time.now\n",
467 "puts \"Found #{found}, sum #{found.map {|f| f.sum}}, in #{finish - start}\""
468 ]
469 },
470 {
471 "cell_type": "code",
472 "execution_count": 34,
473 "metadata": {},
474 "outputs": [
475 {
476 "name": "stdout",
477 "output_type": "stream",
478 "text": [
479 "1 groups, added 2\n",
480 "142 groups, added 233\n",
481 "437 groups, added 547\n",
482 "903 groups, added 877\n",
483 "1420 groups, added 1229\n",
484 "1926 groups, added 1597\n",
485 "2531 groups, added 1993\n",
486 "3201 groups, added 2371\n",
487 "3958 groups, added 2749\n",
488 "4678 groups, added 3187\n",
489 "5565 groups, added 3581\n",
490 "6455 groups, added 4001\n",
491 "7509 groups, added 4421\n",
492 "8594 groups, added 4861\n",
493 "9887 groups, added 5281\n",
494 "11260 groups, added 5701\n",
495 "12844 groups, added 6143\n",
496 "14344 groups, added 6577\n",
497 "16018 groups, added 7001\n",
498 "17704 groups, added 7507\n",
499 "19570 groups, added 7927\n",
500 "21743 groups, added 8389\n",
501 "Found [[13, 5197, 5701, 6733, 8389]], sum [26033], in 387.589197431\n"
502 ]
503 }
504 ],
505 "source": [
506 "start = Time.now\n",
507 "found = grow_to_size2 5, debug=true\n",
508 "finish = Time.now\n",
509 "puts \"Found #{found}, sum #{found.map {|f| f.sum}}, in #{finish - start}\""
510 ]
511 },
512 {
513 "cell_type": "code",
514 "execution_count": 35,
515 "metadata": {},
516 "outputs": [
517 {
518 "data": {
519 "text/plain": [
520 "26033"
521 ]
522 },
523 "execution_count": 35,
524 "metadata": {},
525 "output_type": "execute_result"
526 }
527 ],
528 "source": [
529 "[13, 5197, 5701, 6733, 8389].sum"
530 ]
531 },
532 {
533 "cell_type": "code",
534 "execution_count": null,
535 "metadata": {
536 "collapsed": true
537 },
538 "outputs": [],
539 "source": []
540 }
541 ],
542 "metadata": {
543 "kernelspec": {
544 "display_name": "Ruby 2.4.0",
545 "language": "ruby",
546 "name": "ruby"
547 },
548 "language_info": {
549 "file_extension": ".rb",
550 "mimetype": "application/x-ruby",
551 "name": "ruby",
552 "version": "2.4.0"
553 }
554 },
555 "nbformat": 4,
556 "nbformat_minor": 2
557 }