Done puzzle 64
[project-euler.git] / euler50.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": false,
8 "scrolled": true
9 },
10 "outputs": [
11 {
12 "data": {
13 "text/plain": [
14 "104743"
15 ]
16 },
17 "execution_count": 1,
18 "metadata": {},
19 "output_type": "execute_result"
20 }
21 ],
22 "source": [
23 "load 'primes.rb'\n",
24 "$primes = Primes.instance\n",
25 "$primes[10000] # returns the 10,001st prime"
26 ]
27 },
28 {
29 "cell_type": "code",
30 "execution_count": 2,
31 "metadata": {
32 "collapsed": false
33 },
34 "outputs": [
35 {
36 "data": {
37 "text/plain": [
38 "[2, 3, 5]"
39 ]
40 },
41 "execution_count": 2,
42 "metadata": {},
43 "output_type": "execute_result"
44 }
45 ],
46 "source": [
47 "(0...(0+3)).map{|i| $primes[i]}"
48 ]
49 },
50 {
51 "cell_type": "code",
52 "execution_count": 3,
53 "metadata": {
54 "collapsed": false
55 },
56 "outputs": [
57 {
58 "data": {
59 "text/plain": [
60 ":prime_slice"
61 ]
62 },
63 "execution_count": 3,
64 "metadata": {},
65 "output_type": "execute_result"
66 }
67 ],
68 "source": [
69 "def prime_slice(start, len)\n",
70 " (start...(start + len)).map{|i| $primes[i]}\n",
71 "end"
72 ]
73 },
74 {
75 "cell_type": "code",
76 "execution_count": 4,
77 "metadata": {
78 "collapsed": false
79 },
80 "outputs": [
81 {
82 "data": {
83 "text/plain": [
84 ":sum_primes"
85 ]
86 },
87 "execution_count": 4,
88 "metadata": {},
89 "output_type": "execute_result"
90 }
91 ],
92 "source": [
93 "def sum_primes(start, len)\n",
94 " prime_slice(start, len).sum\n",
95 "end"
96 ]
97 },
98 {
99 "cell_type": "code",
100 "execution_count": 5,
101 "metadata": {
102 "collapsed": false
103 },
104 "outputs": [
105 {
106 "data": {
107 "text/plain": [
108 "[2, 3, 5, 7, 11]"
109 ]
110 },
111 "execution_count": 5,
112 "metadata": {},
113 "output_type": "execute_result"
114 }
115 ],
116 "source": [
117 "prime_slice(0, 5)"
118 ]
119 },
120 {
121 "cell_type": "code",
122 "execution_count": 6,
123 "metadata": {
124 "collapsed": false
125 },
126 "outputs": [
127 {
128 "data": {
129 "text/plain": [
130 "28"
131 ]
132 },
133 "execution_count": 6,
134 "metadata": {},
135 "output_type": "execute_result"
136 }
137 ],
138 "source": [
139 "sum_primes(0, 5)"
140 ]
141 },
142 {
143 "cell_type": "code",
144 "execution_count": 8,
145 "metadata": {
146 "collapsed": false,
147 "scrolled": true
148 },
149 "outputs": [
150 {
151 "data": {
152 "text/plain": [
153 "[3, 543, 997651]"
154 ]
155 },
156 "execution_count": 8,
157 "metadata": {},
158 "output_type": "execute_result"
159 }
160 ],
161 "source": [
162 "sum_limit = 10**6\n",
163 "\n",
164 "start = 0\n",
165 "best_start = 0\n",
166 "best_length = 1\n",
167 "while $primes[start] <= sum_limit\n",
168 " # puts start\n",
169 " length = 1\n",
170 " sp = sum_primes(start, length)\n",
171 " while sp <= sum_limit\n",
172 " if sp.prime? && length > best_length\n",
173 " best_start = start\n",
174 " best_length = length\n",
175 " # puts \"#{start} - #{length} : #{sum_primes(start, length)}\"\n",
176 " end\n",
177 " length += 1\n",
178 " sp = sum_primes(start, length)\n",
179 " end\n",
180 " start += 1\n",
181 "end\n",
182 "[best_start, best_length, sum_primes(best_start, best_length)]"
183 ]
184 },
185 {
186 "cell_type": "code",
187 "execution_count": null,
188 "metadata": {
189 "collapsed": true
190 },
191 "outputs": [],
192 "source": []
193 }
194 ],
195 "metadata": {
196 "kernelspec": {
197 "display_name": "Ruby 2.4.0",
198 "language": "ruby",
199 "name": "ruby"
200 },
201 "language_info": {
202 "file_extension": ".rb",
203 "mimetype": "application/x-ruby",
204 "name": "ruby",
205 "version": "2.4.0"
206 }
207 },
208 "nbformat": 4,
209 "nbformat_minor": 0
210 }