Done puzzle 44
[project-euler.git] / euler30.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 15,
6 "metadata": {
7 "collapsed": false
8 },
9 "outputs": [
10 {
11 "data": {
12 "text/plain": [
13 "Object"
14 ]
15 },
16 "execution_count": 15,
17 "metadata": {},
18 "output_type": "execute_result"
19 }
20 ],
21 "source": [
22 "include Math"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 10,
28 "metadata": {
29 "collapsed": false
30 },
31 "outputs": [
32 {
33 "data": {
34 "text/plain": [
35 "642"
36 ]
37 },
38 "execution_count": 10,
39 "metadata": {},
40 "output_type": "execute_result"
41 }
42 ],
43 "source": [
44 "125.to_s.each_char.map {|d| d.to_i ** 4}.sum"
45 ]
46 },
47 {
48 "cell_type": "code",
49 "execution_count": 11,
50 "metadata": {
51 "collapsed": false
52 },
53 "outputs": [
54 {
55 "data": {
56 "text/plain": [
57 "1634"
58 ]
59 },
60 "execution_count": 11,
61 "metadata": {},
62 "output_type": "execute_result"
63 }
64 ],
65 "source": [
66 "1634.to_s.each_char.map {|d| d.to_i ** 4}.sum"
67 ]
68 },
69 {
70 "cell_type": "code",
71 "execution_count": 16,
72 "metadata": {
73 "collapsed": false
74 },
75 "outputs": [
76 {
77 "data": {
78 "text/plain": [
79 "5.470182551532643"
80 ]
81 },
82 "execution_count": 16,
83 "metadata": {},
84 "output_type": "execute_result"
85 }
86 ],
87 "source": [
88 "log10(5) + 5 * log10(9)"
89 ]
90 },
91 {
92 "cell_type": "code",
93 "execution_count": 18,
94 "metadata": {
95 "collapsed": false
96 },
97 "outputs": [
98 {
99 "name": "stdout",
100 "output_type": "stream",
101 "text": [
102 "0: -Infinity =?= 1\n",
103 "1: 4.771212547196624 =?= 2\n",
104 "2: 5.0722425428606055 =?= 3\n",
105 "3: 5.248333801916287 =?= 4\n",
106 "4: 5.373272538524587 =?= 5\n",
107 "5: 5.470182551532643 =?= 6\n",
108 "6: 5.549363797580268 =?= 7\n",
109 "7: 5.616310587210881 =?= 8\n",
110 "8: 5.674302534188568 =?= 9\n",
111 "9: 5.7254550566359494 =?= 10\n"
112 ]
113 },
114 {
115 "data": {
116 "text/plain": [
117 "10"
118 ]
119 },
120 "execution_count": 18,
121 "metadata": {},
122 "output_type": "execute_result"
123 }
124 ],
125 "source": [
126 "10.times do |n|\n",
127 " puts \"#{n}: #{log10(n) + 5 * log10(9)} =?= #{n+1}\"\n",
128 "end"
129 ]
130 },
131 {
132 "cell_type": "code",
133 "execution_count": 19,
134 "metadata": {
135 "collapsed": false
136 },
137 "outputs": [
138 {
139 "data": {
140 "text/plain": [
141 ":digit_power_sum"
142 ]
143 },
144 "execution_count": 19,
145 "metadata": {},
146 "output_type": "execute_result"
147 }
148 ],
149 "source": [
150 "def digit_power_sum(n, p)\n",
151 " n.to_s.each_char.map {|d| d.to_i ** p}.sum\n",
152 "end"
153 ]
154 },
155 {
156 "cell_type": "code",
157 "execution_count": null,
158 "metadata": {
159 "collapsed": true
160 },
161 "outputs": [],
162 "source": [
163 "(2..10^6)"
164 ]
165 },
166 {
167 "cell_type": "code",
168 "execution_count": 20,
169 "metadata": {
170 "collapsed": false
171 },
172 "outputs": [
173 {
174 "data": {
175 "text/plain": [
176 "642"
177 ]
178 },
179 "execution_count": 20,
180 "metadata": {},
181 "output_type": "execute_result"
182 }
183 ],
184 "source": [
185 "digit_power_sum(125, 4)"
186 ]
187 },
188 {
189 "cell_type": "code",
190 "execution_count": 21,
191 "metadata": {
192 "collapsed": false
193 },
194 "outputs": [
195 {
196 "data": {
197 "text/plain": [
198 "1634"
199 ]
200 },
201 "execution_count": 21,
202 "metadata": {},
203 "output_type": "execute_result"
204 }
205 ],
206 "source": [
207 "digit_power_sum(1634, 4)"
208 ]
209 },
210 {
211 "cell_type": "code",
212 "execution_count": 23,
213 "metadata": {
214 "collapsed": false
215 },
216 "outputs": [
217 {
218 "data": {
219 "text/plain": [
220 "[1634, 8208, 9474]"
221 ]
222 },
223 "execution_count": 23,
224 "metadata": {},
225 "output_type": "execute_result"
226 }
227 ],
228 "source": [
229 "(2..10000).select {|n| digit_power_sum(n, 4) == n}"
230 ]
231 },
232 {
233 "cell_type": "code",
234 "execution_count": 32,
235 "metadata": {
236 "collapsed": false
237 },
238 "outputs": [
239 {
240 "data": {
241 "text/plain": [
242 "[1, 4150, 4151, 54748, 92727, 93084, 194979]"
243 ]
244 },
245 "execution_count": 32,
246 "metadata": {},
247 "output_type": "execute_result"
248 }
249 ],
250 "source": [
251 "(1..10**6).select {|n| digit_power_sum(n, 5) == n}"
252 ]
253 },
254 {
255 "cell_type": "code",
256 "execution_count": 33,
257 "metadata": {
258 "collapsed": false
259 },
260 "outputs": [
261 {
262 "data": {
263 "text/plain": [
264 "[1, 4150, 4151, 54748, 92727, 93084, 194979]"
265 ]
266 },
267 "execution_count": 33,
268 "metadata": {},
269 "output_type": "execute_result"
270 }
271 ],
272 "source": [
273 "(1..10**7).select {|n| digit_power_sum(n, 5) == n}"
274 ]
275 },
276 {
277 "cell_type": "code",
278 "execution_count": 34,
279 "metadata": {
280 "collapsed": false
281 },
282 "outputs": [
283 {
284 "data": {
285 "text/plain": [
286 "443839"
287 ]
288 },
289 "execution_count": 34,
290 "metadata": {},
291 "output_type": "execute_result"
292 }
293 ],
294 "source": [
295 "(2..10**6).select {|n| digit_power_sum(n, 5) == n}.sum"
296 ]
297 },
298 {
299 "cell_type": "code",
300 "execution_count": null,
301 "metadata": {
302 "collapsed": true
303 },
304 "outputs": [],
305 "source": []
306 }
307 ],
308 "metadata": {
309 "kernelspec": {
310 "display_name": "Ruby 2.4.0",
311 "language": "ruby",
312 "name": "ruby"
313 },
314 "language_info": {
315 "file_extension": ".rb",
316 "mimetype": "application/x-ruby",
317 "name": "ruby",
318 "version": "2.4.0"
319 }
320 },
321 "nbformat": 4,
322 "nbformat_minor": 0
323 }