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