Done puzzle 64
[project-euler.git] / euler52.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 'array-numbers.rb'"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 23,
28 "metadata": {
29 "collapsed": false
30 },
31 "outputs": [
32 {
33 "data": {
34 "text/plain": [
35 ":sequence"
36 ]
37 },
38 "execution_count": 23,
39 "metadata": {},
40 "output_type": "execute_result"
41 }
42 ],
43 "source": [
44 "def sequence(n0=0, &generator)\n",
45 " Enumerator.new do |yielder|\n",
46 " n = n0\n",
47 " loop do\n",
48 " yielder.yield generator.call(n)\n",
49 " n = n + 1\n",
50 " end\n",
51 " end\n",
52 "end"
53 ]
54 },
55 {
56 "cell_type": "code",
57 "execution_count": 24,
58 "metadata": {
59 "collapsed": false
60 },
61 "outputs": [
62 {
63 "data": {
64 "text/plain": [
65 "[0, 2, 4, 6, 8]"
66 ]
67 },
68 "execution_count": 24,
69 "metadata": {},
70 "output_type": "execute_result"
71 }
72 ],
73 "source": [
74 "sequence {|n| n * 2}.lazy.take(5).force"
75 ]
76 },
77 {
78 "cell_type": "code",
79 "execution_count": 25,
80 "metadata": {
81 "collapsed": false
82 },
83 "outputs": [
84 {
85 "data": {
86 "text/plain": [
87 "[6, 8, 10, 12, 14]"
88 ]
89 },
90 "execution_count": 25,
91 "metadata": {},
92 "output_type": "execute_result"
93 }
94 ],
95 "source": [
96 "sequence(3) {|n| n * 2}.lazy.take(5).force"
97 ]
98 },
99 {
100 "cell_type": "code",
101 "execution_count": 19,
102 "metadata": {
103 "collapsed": false
104 },
105 "outputs": [
106 {
107 "data": {
108 "text/plain": [
109 "22"
110 ]
111 },
112 "execution_count": 19,
113 "metadata": {},
114 "output_type": "execute_result"
115 }
116 ],
117 "source": [
118 "sequence {|n| n * 2}.lazy.select {|n| n > 20}.first"
119 ]
120 },
121 {
122 "cell_type": "code",
123 "execution_count": 3,
124 "metadata": {
125 "collapsed": false
126 },
127 "outputs": [
128 {
129 "data": {
130 "text/plain": [
131 "[1, 2, 4, 5, 7, 8]"
132 ]
133 },
134 "execution_count": 3,
135 "metadata": {},
136 "output_type": "execute_result"
137 }
138 ],
139 "source": [
140 "125874.to_digits.sort"
141 ]
142 },
143 {
144 "cell_type": "code",
145 "execution_count": 4,
146 "metadata": {
147 "collapsed": false
148 },
149 "outputs": [
150 {
151 "data": {
152 "text/plain": [
153 "[1, 2, 4, 5, 7, 8]"
154 ]
155 },
156 "execution_count": 4,
157 "metadata": {},
158 "output_type": "execute_result"
159 }
160 ],
161 "source": [
162 "(125874 * 2).to_digits.sort"
163 ]
164 },
165 {
166 "cell_type": "code",
167 "execution_count": 9,
168 "metadata": {
169 "collapsed": false
170 },
171 "outputs": [
172 {
173 "data": {
174 "text/plain": [
175 "1"
176 ]
177 },
178 "execution_count": 9,
179 "metadata": {},
180 "output_type": "execute_result"
181 }
182 ],
183 "source": [
184 "[125874.to_digits.sort, (125874 * 2).to_digits.sort, 124578.to_digits.sort].to_set.length"
185 ]
186 },
187 {
188 "cell_type": "code",
189 "execution_count": 11,
190 "metadata": {
191 "collapsed": false
192 },
193 "outputs": [
194 {
195 "data": {
196 "text/plain": [
197 ":identical_multiples"
198 ]
199 },
200 "execution_count": 11,
201 "metadata": {},
202 "output_type": "execute_result"
203 }
204 ],
205 "source": [
206 "def identical_multiples(n, max_multiplier)\n",
207 " (1..max_multiplier).map {|m| (n * m).to_digits.sort}.to_set.length == 1\n",
208 "end"
209 ]
210 },
211 {
212 "cell_type": "code",
213 "execution_count": 18,
214 "metadata": {
215 "collapsed": false
216 },
217 "outputs": [
218 {
219 "data": {
220 "text/plain": [
221 "true"
222 ]
223 },
224 "execution_count": 18,
225 "metadata": {},
226 "output_type": "execute_result"
227 }
228 ],
229 "source": [
230 "identical_multiples(125874, 2)"
231 ]
232 },
233 {
234 "cell_type": "code",
235 "execution_count": 26,
236 "metadata": {
237 "collapsed": false
238 },
239 "outputs": [
240 {
241 "data": {
242 "text/plain": [
243 "142857"
244 ]
245 },
246 "execution_count": 26,
247 "metadata": {},
248 "output_type": "execute_result"
249 }
250 ],
251 "source": [
252 "sequence(1) {|n| n}.lazy.select {|n| identical_multiples n, 6}.first"
253 ]
254 },
255 {
256 "cell_type": "code",
257 "execution_count": null,
258 "metadata": {
259 "collapsed": true
260 },
261 "outputs": [],
262 "source": []
263 }
264 ],
265 "metadata": {
266 "kernelspec": {
267 "display_name": "Ruby 2.4.0",
268 "language": "ruby",
269 "name": "ruby"
270 },
271 "language_info": {
272 "file_extension": ".rb",
273 "mimetype": "application/x-ruby",
274 "name": "ruby",
275 "version": "2.4.0"
276 }
277 },
278 "nbformat": 4,
279 "nbformat_minor": 0
280 }