18 "output_type": "execute_result"
23 "load 'array-numbers.rb'\n",
24 "primes = Primes.instance\n",
43 "output_type": "execute_result"
47 "def replace(num, positions, replace_with)\n",
48 " digits = num.to_digits\n",
49 " positions.each do |p|\n",
50 " digits[p] = replace_with\n",
71 "output_type": "execute_result"
75 "replace(56003, [2, 3], 9)"
80 "execution_count": 38,
88 ":replace_in_positions"
91 "execution_count": 38,
93 "output_type": "execute_result"
97 "def replace_in_positions(num, positions)\n",
98 " (0..9).map {|d| replace num, positions, d}.select {|n| n.to_digits.length == num.to_digits.length}\n",
104 "execution_count": 5,
112 "[56003, 56113, 56223, 56333, 56443, 56553, 56663, 56773, 56883, 56993]"
115 "execution_count": 5,
117 "output_type": "execute_result"
121 "replace_in_positions(56003, [2, 3])"
126 "execution_count": 50,
134 ":count_primes_in_positions"
137 "execution_count": 50,
139 "output_type": "execute_result"
143 "def count_primes_in_positions(num, positions)\n",
144 " others = replace_in_positions(num, positions).select {|n| n.prime?}\n",
145 " if others.include?(num)\n",
155 "execution_count": 52,
166 "execution_count": 52,
168 "output_type": "execute_result"
172 "count_primes_in_positions(56004, [2, 3])"
177 "execution_count": 8,
185 ":best_count_primes_r"
188 "execution_count": 8,
190 "output_type": "execute_result"
194 "def best_count_primes_r(num, n_digits)\n",
195 " possibles = (0...num.to_digits.length).to_a\n",
196 " possibles.combination(n_digits).map {|ps| count_primes_in_positions(num, ps)}.max\n",
202 "execution_count": 9,
213 "execution_count": 9,
215 "output_type": "execute_result"
219 "best_count_primes_r(56003, 2)"
224 "execution_count": 15,
235 "execution_count": 15,
237 "output_type": "execute_result"
241 "best_count_primes_r(3, 1)"
246 "execution_count": 18,
257 "execution_count": 18,
259 "output_type": "execute_result"
263 "def best_count_primes(num)\n",
264 " replacements = (1..num.to_digits.length).to_a\n",
265 " replacements.map {|n| best_count_primes_r(num, n)}.max\n",
271 "execution_count": 21,
282 "execution_count": 21,
284 "output_type": "execute_result"
288 "best_count_primes(56003)"
293 "execution_count": 20,
304 "execution_count": 20,
306 "output_type": "execute_result"
310 "best_count_primes(4)"
315 "execution_count": 26,
326 "execution_count": 26,
328 "output_type": "execute_result"
332 "def sequence(&generator)\n",
333 " Enumerator.new do |yielder|\n",
336 " yielder.yield generator.call(n)\n",
345 "execution_count": 53,
353 "[[2, 4], [3, 4], [5, 4], [7, 4], [11, 5], [13, 6], [17, 5], [19, 5], [23, 6], [29, 5], [31, 5], [37, 5], [41, 5], [43, 6], [47, 5], [53, 6], [59, 5], [61, 5], [67, 5], [71, 5]]"
356 "execution_count": 53,
358 "output_type": "execute_result"
362 "primes.take(20).map {|p| [p, best_count_primes(p)]}"
367 "execution_count": 56,
379 "execution_count": 56,
381 "output_type": "execute_result"
386 "while best_count_primes(primes[i]) < 8 do\n",
394 "execution_count": 55,
405 "execution_count": 55,
407 "output_type": "execute_result"
411 "best_count_primes 120383"
416 "execution_count": 48,
424 "[[333383, 555383, 777383], [110183, 330383, 660683, 880883], [], [330383, 770387, 990389], [323383, 525583, 626683, 929983], [121313, 222323, 323333, 424343, 525353, 626363, 828383, 929393], [323383, 929389], [620663, 720773], [120181], [920399], [], [122323, 166363], [], [100003, 120223, 140443, 160663, 170773, 180883], [], [110311], [], [121181], [], []]"
427 "execution_count": 48,
429 "output_type": "execute_result"
434 "possibles = (0...num.to_digits.length).to_a\n",
435 "possibles.combination(3).map {|ps| replace_in_positions(num, ps).select {|n| n.prime?}}# .map {|ps| count_primes_in_positions(num, ps)}"
440 "execution_count": 25,
451 "execution_count": 25,
453 "output_type": "execute_result"
457 "best_count_primes(121313)"
462 "execution_count": null,
472 "display_name": "Ruby 2.4.0",
477 "file_extension": ".rb",
478 "mimetype": "application/x-ruby",