Made the primes library faster
[project-euler.git] / euler35.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false,
8 "scrolled": true
9 },
10 "outputs": [
11 {
12 "data": {
13 "text/plain": [
14 "#<Primes:0x0055fc1b2516c0 @primes=[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997, 1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097, 1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193, 1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297, 1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399, 1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499, 1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597, 1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699, 1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789, 1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889, 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999, 2003, 2011, 2017, 2027, 2029, 2039, 2053, 2063, 2069, 2081, 2083, 2087, 2089, 2099, 2111, 2113, 2129, 2131, 2137, 2141, 2143, 2153, 2161, 2179, 2203, 2207, 2213, 2221, 2237, 2239, 2243, 2251, 2267, 2269, 2273, 2281, 2287, 2293, 2297, 2309, 2311, 2333, 2339, 2341, 2347, 2351, 2357, 2371, 2377, 2381, 2383, 2389, 2393, 2399, 2411, 2417, 2423, 2437, 2441, 2447, 2459, 2467, 2473, 2477, 2503, 2521, 2531, 2539, 2543, 2549, 2551, 2557, 2579, 2591, 2593, 2609, 2617, 2621, 2633, 2647, 2657, 2659, 2663, 2671, 2677, 2683, 2687, 2689, 2693, 2699, 2707, 2711, 2713, 2719, 2729, 2731, 2741, 2749, 2753, 2767, 2777, 2789, 2791, 2797, 2801, 2803, 2819, 2833, 2837, 2843, 2851, 2857, 2861, 2879, 2887, 2897, 2903, 2909, 2917, 2927, 2939, 2953, 2957, 2963, 2969, 2971, 2999, 3001, 3011, 3019, 3023, 3037, 3041, 3049, 3061, 3067, 3079, 3083, 3089, 3109, 3119, 3121, 3137, 3163, 3167, 3169, 3181, 3187, 3191, 3203, 3209, 3217, 3221, 3229, 3251, 3253, 3257, 3259, 3271, 3299, 3301, 3307, 3313, 3319, 3323, 3329, 3331, 3343, 3347, 3359, 3361, 3371, 3373, 3389, 3391, 3407, 3413, 3433, 3449, 3457, 3461, 3463, 3467, 3469, 3491, 3499, 3511, 3517, 3527, 3529, 3533, 3539, 3541, 3547, 3557, 3559, 3571, 3581, 3583, 3593, 3607, 3613, 3617, 3623, 3631, 3637, 3643, 3659, 3671, 3673, 3677, 3691, 3697, 3701, 3709, 3719, 3727, 3733, 3739, 3761, 3767, 3769, 3779, 3793, 3797, 3803, 3821, 3823, 3833, 3847, 3851, 3853, 3863, 3877, 3881, 3889, 3907, 3911, 3917, 3919, 3923, 3929, 3931, 3943, 3947, 3967, 3989, 4001, 4003, 4007, 4013, 4019, 4021, 4027, 4049, 4051, 4057, 4073, 4079, 4091, 4093, 4099, 4111, 4127, 4129, 4133, 4139, 4153, 4157, 4159, 4177, 4201, 4211, 4217, 4219, 4229, 4231, 4241, 4243, 4253, 4259, 4261, 4271, 4273, 4283, 4289, 4297, 4327, 4337, 4339, 4349, 4357, 4363, 4373, 4391, 4397, 4409, 4421, 4423, 4441, 4447, 4451, 4457, 4463, 4481, 4483, 4493, 4507, 4513, 4517, 4519, 4523, 4547, 4549, 4561, 4567, 4583, 4591, 4597, 4603, 4621, 4637, 4639, 4643, 4649, 4651, 4657, 4663, 4673, 4679, 4691, 4703, 4721, 4723, 4729, 4733, 4751, 4759, 4783, 4787, 4789, 4793, 4799, 4801, 4813, 4817, 4831, 4861, 4871, 4877, 4889, 4903, 4909, 4919, 4931, 4933, 4937, 4943, 4951, 4957, 4967, 4969, 4973, 4987, 4993, 4999, 5003, 5009, 5011, 5021, 5023, 5039, 5051, 5059, 5077, 5081, 5087, 5099, 5101, 5107, 5113, 5119, 5147, 5153, 5167, 5171, 5179, 5189, 5197, 5209, 5227, 5231, 5233, 5237, 5261, 5273, 5279, 5281, 5297, 5303, 5309, 5323, 5333, 5347, 5351, 5381, 5387, 5393, 5399, 5407, 5413, 5417, 5419, 5431, 5437, 5441, 5443, 5449, 5471, 5477, 5479, 5483, 5501, 5503, 5507, 5519, 5521, 5527, 5531, 5557, 5563, 5569, 5573, 5581, 5591, 5623, 5639, 5641, 5647, 5651, 5653, 5657, 5659, 5669, 5683, 5689, 5693, 5701, 5711, 5717, 5737, 5741, 5743, 5749, 5779, 5783, 5791, 5801, 5807, 5813, 5821, 5827, 5839, 5843, 5849, 5851, 5857, 5861, 5867, 5869, 5879, 5881, 5897, 5903, 5923, 5927, 5939, 5953, 5981, 5987, 6007, 6011, 6029, 6037, 6043, 6047, 6053, 6067, 6073, 6079, 6089, 6091, 6101, 6113, 6121, 6131, 6133, 6143, 6151, 6163, 6173, 6197, 6199, 6203, 6211, 6217, 6221, 6229, 6247, 6257, 6263, 6269, 6271, 6277, 6287, 6299, 6301, 6311, 6317, 6323, 6329, 6337, 6343, 6353, 6359, 6361, 6367, 6373, 6379, 6389, 6397, 6421, 6427, 6449, 6451, 6469, 6473, 6481, 6491, 6521, 6529, 6547, 6551, 6553, 6563, 6569, 6571, 6577, 6581, 6599, 6607, 6619, 6637, 6653, 6659, 6661, 6673, 6679, 6689, 6691, 6701, 6703, 6709, 6719, 6733, 6737, 6761, 6763, 6779, 6781, 6791, 6793, 6803, 6823, 6827, 6829, 6833, 6841, 6857, 6863, 6869, 6871, 6883, 6899, 6907, 6911, 6917, 6947, 6949, 6959, 6961, 6967, 6971, 6977, 6983, 6991, 6997, 7001, 7013, 7019, 7027, 7039, 7043, 7057, 7069, 7079, 7103, 7109, 7121, 7127, 7129, 7151, 7159, 7177, 7187, 7193, 7207, 7211, 7213, 7219, 7229, 7237, 7243, 7247, 7253, 7283, 7297, 7307, 7309, 7321, 7331, 7333, 7349, 7351, 7369, 7393, 7411, 7417, 7433, 7451, 7457, 7459, 7477, 7481, 7487, 7489, 7499, 7507, 7517, 7523, 7529, 7537, 7541, 7547, 7549, 7559, 7561, 7573, 7577, 7583, 7589, 7591, 7603, 7607, 7621, 7639, 7643, 7649, 7669, 7673, 7681, 7687, 7691, 7699, 7703, 7717, 7723, 7727, 7741, 7753, 7757, 7759, 7789, 7793, 7817, 7823, 7829, 7841, 7853, 7867, 7873, 7877, 7879, 7883, 7901, 7907, 7919, 7927, 7933, 7937, 7949, 7951, 7963, 7993, 8009, 8011, 8017, 8039, 8053, 8059, 8069, 8081, 8087, 8089, 8093, 8101, 8111, 8117, 8123, 8147, 8161, 8167, 8171, 8179, 8191, 8209, 8219, 8221, 8231, 8233, 8237, 8243, 8263, 8269, 8273, 8287, 8291, 8293, 8297, 8311, 8317, 8329, 8353, 8363, 8369, 8377, 8387, 8389, 8419, 8423, 8429, 8431, 8443, 8447, 8461, 8467, 8501, 8513, 8521, 8527, 8537, 8539, 8543, 8563, 8573, 8581, 8597, 8599, 8609, 8623, 8627, 8629, 8641, 8647, 8663, 8669, 8677, 8681, 8689, 8693, 8699, 8707, 8713, 8719, 8731, 8737, 8741, 8747, 8753, 8761, 8779, 8783, 8803, 8807, 8819, 8821, 8831, 8837, 8839, 8849, 8861, 8863, 8867, 8887, 8893, 8923, 8929, 8933, 8941, 8951, 8963, 8969, 8971, 8999, 9001, 9007, 9011, 9013, 9029, 9041, 9043, 9049, 9059, 9067, 9091, 9103, 9109, 9127, 9133, 9137, 9151, 9157, 9161, 9173, 9181, 9187, 9199, 9203, 9209, 9221, 9227, 9239, 9241, 9257, 9277, 9281, 9283, 9293, 9311, 9319, 9323, 9337, 9341, 9343, 9349, 9371, 9377, 9391, 9397, 9403, 9413, 9419, 9421, 9431, 9433, 9437, 9439, 9461, 9463, 9467, 9473, 9479, 9491, 9497, 9511, 9521, 9533, 9539, 9547, 9551, 9587, 9601, 9613, 9619, 9623, 9629, 9631, 9643, 9649, 9661, 9677, 9679, 9689, 9697, 9719, 9721, 9733, 9739, 9743, 9749, 9767, 9769, 9781, 9787, 9791, 9803, 9811, 9817, 9829, 9833, 9839, 9851, 9857, 9859, 9871, 9883, 9887, 9901, 9907, 9923, 9929, 9931, 9941, 9949, 9967, 9973], @cache_up_to=30, @mod_candidates=[1, 7, 11, 13, 17, 19, 23, 29]>"
15 ]
16 },
17 "execution_count": 1,
18 "metadata": {},
19 "output_type": "execute_result"
20 }
21 ],
22 "source": [
23 "load 'primes.rb'\n",
24 "$primes = Primes.instance"
25 ]
26 },
27 {
28 "cell_type": "code",
29 "execution_count": 2,
30 "metadata": {
31 "collapsed": false
32 },
33 "outputs": [
34 {
35 "data": {
36 "text/plain": [
37 ":to_r"
38 ]
39 },
40 "execution_count": 2,
41 "metadata": {},
42 "output_type": "execute_result"
43 }
44 ],
45 "source": [
46 "class Array\n",
47 " def to_i\n",
48 " self.map {|d| d.to_s}.join.to_i\n",
49 " end\n",
50 " \n",
51 " def to_r\n",
52 " Rational(self[0].to_i, self[1].to_i)\n",
53 " end\n",
54 "end"
55 ]
56 },
57 {
58 "cell_type": "code",
59 "execution_count": 3,
60 "metadata": {
61 "collapsed": false
62 },
63 "outputs": [
64 {
65 "data": {
66 "text/plain": [
67 ":to_digits"
68 ]
69 },
70 "execution_count": 3,
71 "metadata": {},
72 "output_type": "execute_result"
73 }
74 ],
75 "source": [
76 "class Integer\n",
77 " def to_digits\n",
78 " self.to_s.split('').map {|d| d.to_i}\n",
79 " end\n",
80 "end "
81 ]
82 },
83 {
84 "cell_type": "code",
85 "execution_count": 4,
86 "metadata": {
87 "collapsed": false
88 },
89 "outputs": [
90 {
91 "data": {
92 "text/plain": [
93 ":all_rotations"
94 ]
95 },
96 "execution_count": 4,
97 "metadata": {},
98 "output_type": "execute_result"
99 }
100 ],
101 "source": [
102 "class Array\n",
103 " def all_rotations\n",
104 " (0...self.length).map {|r| self.rotate(r)}\n",
105 " end\n",
106 "end"
107 ]
108 },
109 {
110 "cell_type": "code",
111 "execution_count": 5,
112 "metadata": {
113 "collapsed": false
114 },
115 "outputs": [
116 {
117 "data": {
118 "text/plain": [
119 "[[1, 2, 3], [2, 3, 1], [3, 1, 2]]"
120 ]
121 },
122 "execution_count": 5,
123 "metadata": {},
124 "output_type": "execute_result"
125 }
126 ],
127 "source": [
128 "[1,2,3].all_rotations"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": 6,
134 "metadata": {
135 "collapsed": false
136 },
137 "outputs": [
138 {
139 "data": {
140 "text/plain": [
141 "[123, 231, 312]"
142 ]
143 },
144 "execution_count": 6,
145 "metadata": {},
146 "output_type": "execute_result"
147 }
148 ],
149 "source": [
150 "[1,2,3].all_rotations.map {|n| n.to_i}"
151 ]
152 },
153 {
154 "cell_type": "code",
155 "execution_count": 7,
156 "metadata": {
157 "collapsed": false
158 },
159 "outputs": [
160 {
161 "data": {
162 "text/plain": [
163 "[false, false, false]"
164 ]
165 },
166 "execution_count": 7,
167 "metadata": {},
168 "output_type": "execute_result"
169 }
170 ],
171 "source": [
172 "[1,2,3].all_rotations.map {|n| n.to_i}.map {|n| $primes.include? n}"
173 ]
174 },
175 {
176 "cell_type": "code",
177 "execution_count": 8,
178 "metadata": {
179 "collapsed": false
180 },
181 "outputs": [
182 {
183 "data": {
184 "text/plain": [
185 "[true, true]"
186 ]
187 },
188 "execution_count": 8,
189 "metadata": {},
190 "output_type": "execute_result"
191 }
192 ],
193 "source": [
194 "17.to_digits.all_rotations.map {|n| n.to_i}.map {|n| $primes.include? n}"
195 ]
196 },
197 {
198 "cell_type": "code",
199 "execution_count": 9,
200 "metadata": {
201 "collapsed": false
202 },
203 "outputs": [
204 {
205 "data": {
206 "text/plain": [
207 "false"
208 ]
209 },
210 "execution_count": 9,
211 "metadata": {},
212 "output_type": "execute_result"
213 }
214 ],
215 "source": [
216 "[1,2,3].all_rotations.map {|n| n.to_i}.all? {|n| $primes.include? n}"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": 10,
222 "metadata": {
223 "collapsed": false
224 },
225 "outputs": [
226 {
227 "data": {
228 "text/plain": [
229 "true"
230 ]
231 },
232 "execution_count": 10,
233 "metadata": {},
234 "output_type": "execute_result"
235 }
236 ],
237 "source": [
238 "17.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| $primes.include? n}"
239 ]
240 },
241 {
242 "cell_type": "code",
243 "execution_count": 11,
244 "metadata": {
245 "collapsed": false
246 },
247 "outputs": [
248 {
249 "data": {
250 "text/plain": [
251 ":circular_prime?"
252 ]
253 },
254 "execution_count": 11,
255 "metadata": {},
256 "output_type": "execute_result"
257 }
258 ],
259 "source": [
260 "class Integer\n",
261 " @@primes = Primes.instance\n",
262 " \n",
263 " def prime?\n",
264 " @@primes.include? self\n",
265 " end\n",
266 " \n",
267 " def pl\n",
268 " @@primes.primes.length\n",
269 " end\n",
270 " \n",
271 " def circular_prime?\n",
272 " self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| @@primes.include? n} if self.prime?\n",
273 " end\n",
274 "end"
275 ]
276 },
277 {
278 "cell_type": "code",
279 "execution_count": 11,
280 "metadata": {
281 "collapsed": false
282 },
283 "outputs": [
284 {
285 "data": {
286 "text/plain": [
287 ":circular_prime?"
288 ]
289 },
290 "execution_count": 11,
291 "metadata": {},
292 "output_type": "execute_result"
293 }
294 ],
295 "source": [
296 "class Integer\n",
297 " @@primes = Primes.instance\n",
298 " \n",
299 " def prime?\n",
300 " @@primes.include? self\n",
301 " end\n",
302 " \n",
303 " def pl\n",
304 " @@primes.primes.length\n",
305 " end\n",
306 " \n",
307 " def circular_prime?\n",
308 " self.to_digits.all_rotations.map {|n| n.to_i}.all? {|n| n.prime?} if self.prime?\n",
309 " end\n",
310 "end"
311 ]
312 },
313 {
314 "cell_type": "code",
315 "execution_count": 12,
316 "metadata": {
317 "collapsed": false
318 },
319 "outputs": [
320 {
321 "data": {
322 "text/plain": [
323 "1229"
324 ]
325 },
326 "execution_count": 12,
327 "metadata": {},
328 "output_type": "execute_result"
329 }
330 ],
331 "source": [
332 "0.pl"
333 ]
334 },
335 {
336 "cell_type": "code",
337 "execution_count": 13,
338 "metadata": {
339 "collapsed": false
340 },
341 "outputs": [
342 {
343 "data": {
344 "text/plain": [
345 "[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]"
346 ]
347 },
348 "execution_count": 13,
349 "metadata": {},
350 "output_type": "execute_result"
351 }
352 ],
353 "source": [
354 "(2..100).select {|n| n.circular_prime?}"
355 ]
356 },
357 {
358 "cell_type": "code",
359 "execution_count": 14,
360 "metadata": {
361 "collapsed": false
362 },
363 "outputs": [
364 {
365 "data": {
366 "text/plain": [
367 "[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97]"
368 ]
369 },
370 "execution_count": 14,
371 "metadata": {},
372 "output_type": "execute_result"
373 }
374 ],
375 "source": [
376 "(2..100).select {|n| n.circular_prime?}"
377 ]
378 },
379 {
380 "cell_type": "code",
381 "execution_count": 15,
382 "metadata": {
383 "collapsed": false
384 },
385 "outputs": [
386 {
387 "data": {
388 "text/plain": [
389 "13"
390 ]
391 },
392 "execution_count": 15,
393 "metadata": {},
394 "output_type": "execute_result"
395 }
396 ],
397 "source": [
398 "(2..100).select {|n| n.circular_prime?}.length"
399 ]
400 },
401 {
402 "cell_type": "code",
403 "execution_count": 16,
404 "metadata": {
405 "collapsed": false
406 },
407 "outputs": [],
408 "source": [
409 "# ps = $primes.take_while {|i| i <= 10**6}\n",
410 "# ps.length"
411 ]
412 },
413 {
414 "cell_type": "code",
415 "execution_count": 17,
416 "metadata": {
417 "collapsed": false
418 },
419 "outputs": [],
420 "source": [
421 "# (2..10**4).select {|n| n.circular_prime?}"
422 ]
423 },
424 {
425 "cell_type": "code",
426 "execution_count": 18,
427 "metadata": {
428 "collapsed": false
429 },
430 "outputs": [
431 {
432 "name": "stdout",
433 "output_type": "stream",
434 "text": [
435 "Time elapsed 0.125950399 seconds, going from 1229 to 1230 primes cached\n"
436 ]
437 },
438 {
439 "data": {
440 "text/plain": [
441 "33"
442 ]
443 },
444 "execution_count": 18,
445 "metadata": {},
446 "output_type": "execute_result"
447 }
448 ],
449 "source": [
450 "pl0 = 0.pl\n",
451 "beginning_time = Time.now\n",
452 "cps = (2..10**4).select {|n| n.circular_prime?}\n",
453 "end_time = Time.now\n",
454 "pl1 = 0.pl\n",
455 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
456 "cps.length"
457 ]
458 },
459 {
460 "cell_type": "code",
461 "execution_count": 19,
462 "metadata": {
463 "collapsed": false
464 },
465 "outputs": [
466 {
467 "name": "stdout",
468 "output_type": "stream",
469 "text": [
470 "Time elapsed 0.123380027 seconds, going from 1230 to 1230 primes cached\n"
471 ]
472 },
473 {
474 "data": {
475 "text/plain": [
476 "33"
477 ]
478 },
479 "execution_count": 19,
480 "metadata": {},
481 "output_type": "execute_result"
482 }
483 ],
484 "source": [
485 "pl0 = 0.pl\n",
486 "beginning_time = Time.now\n",
487 "cps = (2..10**4).select {|n| n.circular_prime?}\n",
488 "end_time = Time.now\n",
489 "pl1 = 0.pl\n",
490 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
491 "cps.length"
492 ]
493 },
494 {
495 "cell_type": "code",
496 "execution_count": 20,
497 "metadata": {
498 "collapsed": false
499 },
500 "outputs": [
501 {
502 "name": "stdout",
503 "output_type": "stream",
504 "text": [
505 "Time elapsed 9.59874264 seconds, going from 1230 to 9593 primes cached\n"
506 ]
507 },
508 {
509 "data": {
510 "text/plain": [
511 "43"
512 ]
513 },
514 "execution_count": 20,
515 "metadata": {},
516 "output_type": "execute_result"
517 }
518 ],
519 "source": [
520 "pl0 = 0.pl\n",
521 "beginning_time = Time.now\n",
522 "cps = (2..10**5).select {|n| n.circular_prime?}\n",
523 "end_time = Time.now\n",
524 "pl1 = 0.pl\n",
525 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
526 "cps.length"
527 ]
528 },
529 {
530 "cell_type": "code",
531 "execution_count": 21,
532 "metadata": {
533 "collapsed": false
534 },
535 "outputs": [
536 {
537 "name": "stdout",
538 "output_type": "stream",
539 "text": [
540 "Time elapsed 7.768858416 seconds, going from 9593 to 9593 primes cached\n"
541 ]
542 },
543 {
544 "data": {
545 "text/plain": [
546 "43"
547 ]
548 },
549 "execution_count": 21,
550 "metadata": {},
551 "output_type": "execute_result"
552 }
553 ],
554 "source": [
555 "pl0 = 0.pl\n",
556 "beginning_time = Time.now\n",
557 "cps = (2..10**5).select {|n| n.circular_prime?}\n",
558 "end_time = Time.now\n",
559 "pl1 = 0.pl\n",
560 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
561 "cps.length"
562 ]
563 },
564 {
565 "cell_type": "code",
566 "execution_count": 22,
567 "metadata": {
568 "collapsed": false
569 },
570 "outputs": [
571 {
572 "name": "stdout",
573 "output_type": "stream",
574 "text": [
575 "Time elapsed 710.264030183 seconds, going from 9593 to 78499 primes cached\n"
576 ]
577 },
578 {
579 "data": {
580 "text/plain": [
581 "55"
582 ]
583 },
584 "execution_count": 22,
585 "metadata": {},
586 "output_type": "execute_result"
587 }
588 ],
589 "source": [
590 "pl0 = 0.pl\n",
591 "beginning_time = Time.now\n",
592 "cps = (2..10**6).select {|n| n.circular_prime?}\n",
593 "end_time = Time.now\n",
594 "pl1 = 0.pl\n",
595 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
596 "cps.length"
597 ]
598 },
599 {
600 "cell_type": "code",
601 "execution_count": 23,
602 "metadata": {
603 "collapsed": false
604 },
605 "outputs": [
606 {
607 "name": "stdout",
608 "output_type": "stream",
609 "text": [
610 "Time elapsed 597.009405584 seconds, going from 78499 to 78499 primes cached\n"
611 ]
612 },
613 {
614 "data": {
615 "text/plain": [
616 "55"
617 ]
618 },
619 "execution_count": 23,
620 "metadata": {},
621 "output_type": "execute_result"
622 }
623 ],
624 "source": [
625 "pl0 = 0.pl\n",
626 "beginning_time = Time.now\n",
627 "cps = (2..10**6).select {|n| n.circular_prime?}\n",
628 "end_time = Time.now\n",
629 "pl1 = 0.pl\n",
630 "puts \"Time elapsed #{(end_time - beginning_time)} seconds, going from #{pl0} to #{pl1} primes cached\"\n",
631 "cps.length"
632 ]
633 },
634 {
635 "cell_type": "code",
636 "execution_count": null,
637 "metadata": {
638 "collapsed": true
639 },
640 "outputs": [],
641 "source": []
642 }
643 ],
644 "metadata": {
645 "kernelspec": {
646 "display_name": "Ruby 2.4.0",
647 "language": "ruby",
648 "name": "ruby"
649 },
650 "language_info": {
651 "file_extension": ".rb",
652 "mimetype": "application/x-ruby",
653 "name": "ruby",
654 "version": "2.4.0"
655 }
656 },
657 "nbformat": 4,
658 "nbformat_minor": 0
659 }