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