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