Done puzzle 64
[project-euler.git] / euler34.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 4,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [],
10 "source": [
11 "class Integer\n",
12 " def factorial\n",
13 " (1..self).inject(:*) || 1\n",
14 " end\n",
15 " alias :! :factorial\n",
16 "end"
17 ]
18 },
19 {
20 "cell_type": "code",
21 "execution_count": 11,
22 "metadata": {
23 "collapsed": false
24 },
25 "outputs": [
26 {
27 "data": {
28 "text/plain": [
29 ":to_digits"
30 ]
31 },
32 "execution_count": 11,
33 "metadata": {},
34 "output_type": "execute_result"
35 }
36 ],
37 "source": [
38 "class Numeric\n",
39 " def to_digits\n",
40 " self.to_s.split('').map {|d| d.to_i}\n",
41 " end\n",
42 "end "
43 ]
44 },
45 {
46 "cell_type": "code",
47 "execution_count": 5,
48 "metadata": {
49 "collapsed": false
50 },
51 "outputs": [
52 {
53 "data": {
54 "text/plain": [
55 "120"
56 ]
57 },
58 "execution_count": 5,
59 "metadata": {},
60 "output_type": "execute_result"
61 }
62 ],
63 "source": [
64 "5.factorial"
65 ]
66 },
67 {
68 "cell_type": "code",
69 "execution_count": 6,
70 "metadata": {
71 "collapsed": false
72 },
73 "outputs": [
74 {
75 "data": {
76 "text/plain": [
77 "362880"
78 ]
79 },
80 "execution_count": 6,
81 "metadata": {},
82 "output_type": "execute_result"
83 }
84 ],
85 "source": [
86 "9.factorial"
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": 8,
92 "metadata": {
93 "collapsed": false
94 },
95 "outputs": [
96 {
97 "data": {
98 "text/plain": [
99 "362880"
100 ]
101 },
102 "execution_count": 8,
103 "metadata": {},
104 "output_type": "execute_result"
105 }
106 ],
107 "source": [
108 "9.!"
109 ]
110 },
111 {
112 "cell_type": "code",
113 "execution_count": 10,
114 "metadata": {
115 "collapsed": false
116 },
117 "outputs": [
118 {
119 "name": "stdout",
120 "output_type": "stream",
121 "text": [
122 "1, 362880, 10, false\n",
123 "2, 725760, 100, false\n",
124 "3, 1088640, 1000, false\n",
125 "4, 1451520, 10000, false\n",
126 "5, 1814400, 100000, false\n",
127 "6, 2177280, 1000000, false\n",
128 "7, 2540160, 10000000, true\n",
129 "8, 2903040, 100000000, true\n",
130 "9, 3265920, 1000000000, true\n",
131 "10, 3628800, 10000000000, true\n"
132 ]
133 },
134 {
135 "data": {
136 "text/plain": [
137 "1..10"
138 ]
139 },
140 "execution_count": 10,
141 "metadata": {},
142 "output_type": "execute_result"
143 }
144 ],
145 "source": [
146 "(1..10).each do |i|\n",
147 " puts \"#{i}, #{i * 9.!}, #{10 ** i}, #{i * 9.! < 10 ** i}\"\n",
148 "end"
149 ]
150 },
151 {
152 "cell_type": "code",
153 "execution_count": 12,
154 "metadata": {
155 "collapsed": false
156 },
157 "outputs": [
158 {
159 "data": {
160 "text/plain": [
161 "[3, 4, 5, 6, 7, 8, 9, 10]"
162 ]
163 },
164 "execution_count": 12,
165 "metadata": {},
166 "output_type": "execute_result"
167 }
168 ],
169 "source": [
170 "(3..10).to_a"
171 ]
172 },
173 {
174 "cell_type": "code",
175 "execution_count": 13,
176 "metadata": {
177 "collapsed": false
178 },
179 "outputs": [
180 {
181 "data": {
182 "text/plain": [
183 ":factorial_sum?"
184 ]
185 },
186 "execution_count": 13,
187 "metadata": {},
188 "output_type": "execute_result"
189 }
190 ],
191 "source": [
192 "def factorial_sum?(n)\n",
193 " n.to_digits.map {|d| d.!}.reduce(:+) == n\n",
194 "end"
195 ]
196 },
197 {
198 "cell_type": "code",
199 "execution_count": 14,
200 "metadata": {
201 "collapsed": false
202 },
203 "outputs": [
204 {
205 "data": {
206 "text/plain": [
207 "145"
208 ]
209 },
210 "execution_count": 14,
211 "metadata": {},
212 "output_type": "execute_result"
213 }
214 ],
215 "source": [
216 "145.to_digits.map {|d| d.!}.reduce(:+)"
217 ]
218 },
219 {
220 "cell_type": "code",
221 "execution_count": 15,
222 "metadata": {
223 "collapsed": false
224 },
225 "outputs": [
226 {
227 "data": {
228 "text/plain": [
229 "745"
230 ]
231 },
232 "execution_count": 15,
233 "metadata": {},
234 "output_type": "execute_result"
235 }
236 ],
237 "source": [
238 "146.to_digits.map {|d| d.!}.reduce(:+)"
239 ]
240 },
241 {
242 "cell_type": "code",
243 "execution_count": 16,
244 "metadata": {
245 "collapsed": false
246 },
247 "outputs": [
248 {
249 "data": {
250 "text/plain": [
251 "120"
252 ]
253 },
254 "execution_count": 16,
255 "metadata": {},
256 "output_type": "execute_result"
257 }
258 ],
259 "source": [
260 "5.!"
261 ]
262 },
263 {
264 "cell_type": "code",
265 "execution_count": 17,
266 "metadata": {
267 "collapsed": false
268 },
269 "outputs": [
270 {
271 "data": {
272 "text/plain": [
273 "720"
274 ]
275 },
276 "execution_count": 17,
277 "metadata": {},
278 "output_type": "execute_result"
279 }
280 ],
281 "source": [
282 "6.!"
283 ]
284 },
285 {
286 "cell_type": "code",
287 "execution_count": 18,
288 "metadata": {
289 "collapsed": false
290 },
291 "outputs": [
292 {
293 "data": {
294 "text/plain": [
295 "[145]"
296 ]
297 },
298 "execution_count": 18,
299 "metadata": {},
300 "output_type": "execute_result"
301 }
302 ],
303 "source": [
304 "(3..1000).select {|n| factorial_sum? n}.to_a"
305 ]
306 },
307 {
308 "cell_type": "code",
309 "execution_count": 19,
310 "metadata": {
311 "collapsed": false
312 },
313 "outputs": [
314 {
315 "data": {
316 "text/plain": [
317 "[145, 40585]"
318 ]
319 },
320 "execution_count": 19,
321 "metadata": {},
322 "output_type": "execute_result"
323 }
324 ],
325 "source": [
326 "(3..10**7).select {|n| factorial_sum? n}.to_a"
327 ]
328 },
329 {
330 "cell_type": "code",
331 "execution_count": 20,
332 "metadata": {
333 "collapsed": false
334 },
335 "outputs": [
336 {
337 "data": {
338 "text/plain": [
339 "40730"
340 ]
341 },
342 "execution_count": 20,
343 "metadata": {},
344 "output_type": "execute_result"
345 }
346 ],
347 "source": [
348 "(3..10**7).select {|n| factorial_sum? n}.sum"
349 ]
350 },
351 {
352 "cell_type": "code",
353 "execution_count": null,
354 "metadata": {
355 "collapsed": true
356 },
357 "outputs": [],
358 "source": []
359 }
360 ],
361 "metadata": {
362 "kernelspec": {
363 "display_name": "Ruby 2.4.0",
364 "language": "ruby",
365 "name": "ruby"
366 },
367 "language_info": {
368 "file_extension": ".rb",
369 "mimetype": "application/x-ruby",
370 "name": "ruby",
371 "version": "2.4.0"
372 }
373 },
374 "nbformat": 4,
375 "nbformat_minor": 0
376 }