Started on puzzle 44
[project-euler.git] / euler37.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "true"
14 ]
15 },
16 "execution_count": 1,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "load 'primes.rb'\n",
23 "load 'array-numbers.rb'"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": 4,
29 "metadata": {
30 "collapsed": false
31 },
32 "outputs": [
33 {
34 "data": {
35 "text/plain": [
36 ":truncations"
37 ]
38 },
39 "execution_count": 4,
40 "metadata": {},
41 "output_type": "execute_result"
42 }
43 ],
44 "source": [
45 "def truncations(n)\n",
46 " ds = n.to_digits\n",
47 " dsl = ds.length\n",
48 " (0...(dsl-1)).flat_map {|i| [ds[0..i].to_i, ds[(i+1)..dsl].to_i]}\n",
49 "end"
50 ]
51 },
52 {
53 "cell_type": "code",
54 "execution_count": 5,
55 "metadata": {
56 "collapsed": false
57 },
58 "outputs": [
59 {
60 "data": {
61 "text/plain": [
62 "[1, 23, 12, 3]"
63 ]
64 },
65 "execution_count": 5,
66 "metadata": {},
67 "output_type": "execute_result"
68 }
69 ],
70 "source": [
71 "truncations(123)"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": 6,
77 "metadata": {
78 "collapsed": false
79 },
80 "outputs": [
81 {
82 "data": {
83 "text/plain": [
84 ":truncatable_prime"
85 ]
86 },
87 "execution_count": 6,
88 "metadata": {},
89 "output_type": "execute_result"
90 }
91 ],
92 "source": [
93 "def truncatable_prime(n)\n",
94 " n.prime? && truncations(n).all? {|i| i.prime?}\n",
95 "end"
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": 7,
101 "metadata": {
102 "collapsed": false
103 },
104 "outputs": [
105 {
106 "data": {
107 "text/plain": [
108 "true"
109 ]
110 },
111 "execution_count": 7,
112 "metadata": {},
113 "output_type": "execute_result"
114 }
115 ],
116 "source": [
117 "truncatable_prime(3797)"
118 ]
119 },
120 {
121 "cell_type": "code",
122 "execution_count": 8,
123 "metadata": {
124 "collapsed": false
125 },
126 "outputs": [
127 {
128 "data": {
129 "text/plain": [
130 "false"
131 ]
132 },
133 "execution_count": 8,
134 "metadata": {},
135 "output_type": "execute_result"
136 }
137 ],
138 "source": [
139 "truncatable_prime(3799)"
140 ]
141 },
142 {
143 "cell_type": "code",
144 "execution_count": 9,
145 "metadata": {
146 "collapsed": false
147 },
148 "outputs": [
149 {
150 "data": {
151 "text/plain": [
152 "[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]"
153 ]
154 },
155 "execution_count": 9,
156 "metadata": {},
157 "output_type": "execute_result"
158 }
159 ],
160 "source": [
161 "(10..(10**6)).select {|n| truncatable_prime(n)}.to_a"
162 ]
163 },
164 {
165 "cell_type": "code",
166 "execution_count": 10,
167 "metadata": {
168 "collapsed": false
169 },
170 "outputs": [
171 {
172 "data": {
173 "text/plain": [
174 "[23, 37, 53, 73, 313, 317, 373, 797, 3137, 3797, 739397]"
175 ]
176 },
177 "execution_count": 10,
178 "metadata": {},
179 "output_type": "execute_result"
180 }
181 ],
182 "source": [
183 "(10..(10**10)).lazy.select {|n| truncatable_prime(n)}.take(11).to_a"
184 ]
185 },
186 {
187 "cell_type": "code",
188 "execution_count": 11,
189 "metadata": {
190 "collapsed": false
191 },
192 "outputs": [
193 {
194 "data": {
195 "text/plain": [
196 "748317"
197 ]
198 },
199 "execution_count": 11,
200 "metadata": {},
201 "output_type": "execute_result"
202 }
203 ],
204 "source": [
205 "(10..(10**7)).lazy.select {|n| truncatable_prime(n)}.take(11).sum"
206 ]
207 },
208 {
209 "cell_type": "code",
210 "execution_count": null,
211 "metadata": {
212 "collapsed": true
213 },
214 "outputs": [],
215 "source": []
216 }
217 ],
218 "metadata": {
219 "kernelspec": {
220 "display_name": "Ruby 2.4.0",
221 "language": "ruby",
222 "name": "ruby"
223 },
224 "language_info": {
225 "file_extension": ".rb",
226 "mimetype": "application/x-ruby",
227 "name": "ruby",
228 "version": "2.4.0"
229 }
230 },
231 "nbformat": 4,
232 "nbformat_minor": 0
233 }