+{
+ "cells": [
+ {
+ "cell_type": "code",
+ "execution_count": 12,
+ "metadata": {
+ "collapsed": false,
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "104743"
+ ]
+ },
+ "execution_count": 12,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "load 'primes.rb'\n",
+ "$primes = Primes.instance\n",
+ "$primes[10000] # returns the 10,001st prime"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[2, 3, 5]"
+ ]
+ },
+ "execution_count": 13,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "(0...(0+3)).map{|i| $primes[i]}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 17,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":prime_slice"
+ ]
+ },
+ "execution_count": 17,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def prime_slice(start, len)\n",
+ " (start...(start + len)).map{|i| $primes[i]}\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ ":sum_primes"
+ ]
+ },
+ "execution_count": 18,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "def sum_primes(start, len)\n",
+ " prime_slice(start, len).sum\n",
+ "end"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 20,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[2, 3, 5, 7, 11]"
+ ]
+ },
+ "execution_count": 20,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "prime_slice(0, 5)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 19,
+ "metadata": {
+ "collapsed": false
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "28"
+ ]
+ },
+ "execution_count": 19,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "sum_primes(0, 5)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 30,
+ "metadata": {
+ "collapsed": false,
+ "scrolled": true
+ },
+ "outputs": [
+ {
+ "name": "stdout",
+ "output_type": "stream",
+ "text": [
+ "0 - 2 : 5\n",
+ "0 - 4 : 17\n",
+ "0 - 6 : 41\n",
+ "0 - 12 : 197\n",
+ "0 - 14 : 281\n",
+ "0 - 60 : 7699\n",
+ "0 - 64 : 8893\n",
+ "0 - 96 : 22039\n",
+ "0 - 100 : 24133\n",
+ "0 - 102 : 25237\n",
+ "0 - 108 : 28697\n",
+ "0 - 114 : 32353\n",
+ "0 - 122 : 37561\n",
+ "0 - 124 : 38921\n",
+ "0 - 130 : 43201\n",
+ "0 - 132 : 44683\n",
+ "0 - 146 : 55837\n",
+ "0 - 152 : 61027\n",
+ "0 - 158 : 66463\n",
+ "0 - 162 : 70241\n",
+ "0 - 178 : 86453\n",
+ "0 - 192 : 102001\n",
+ "0 - 198 : 109147\n",
+ "0 - 204 : 116533\n",
+ "0 - 206 : 119069\n",
+ "0 - 208 : 121631\n",
+ "0 - 214 : 129419\n",
+ "0 - 216 : 132059\n",
+ "0 - 296 : 263171\n",
+ "0 - 308 : 287137\n",
+ "0 - 326 : 325019\n",
+ "0 - 328 : 329401\n",
+ "0 - 330 : 333821\n",
+ "0 - 332 : 338279\n",
+ "0 - 334 : 342761\n",
+ "0 - 342 : 360979\n",
+ "0 - 350 : 379667\n",
+ "0 - 356 : 393961\n",
+ "0 - 358 : 398771\n",
+ "0 - 426 : 581921\n",
+ "0 - 446 : 642869\n",
+ "0 - 458 : 681257\n",
+ "0 - 460 : 687767\n",
+ "0 - 464 : 700897\n",
+ "0 - 480 : 754573\n",
+ "0 - 484 : 768373\n",
+ "0 - 488 : 782263\n",
+ "0 - 512 : 868151\n",
+ "0 - 530 : 935507\n",
+ "0 - 536 : 958577\n",
+ "2 - 537 : 970219\n",
+ "2 - 539 : 978037\n",
+ "3 - 543 : 997651\n"
+ ]
+ },
+ {
+ "data": {
+ "text/plain": [
+ "[3, 543, 997651]"
+ ]
+ },
+ "execution_count": 30,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "sum_limit = 10**6\n",
+ "\n",
+ "start = 0\n",
+ "best_start = 0\n",
+ "best_length = 1\n",
+ "while primes[start] <= sum_limit\n",
+ " # puts start\n",
+ " length = 1\n",
+ " sp = sum_primes(start, length)\n",
+ " while sp <= sum_limit\n",
+ " if sp.prime? && length > best_length\n",
+ " best_start = start\n",
+ " best_length = length\n",
+ " # puts \"#{start} - #{length} : #{sum_primes(start, length)}\"\n",
+ " end\n",
+ " length += 1\n",
+ " sp = sum_primes(start, length)\n",
+ " end\n",
+ " start += 1\n",
+ "end\n",
+ "[best_start, best_length, sum_primes(best_start, best_length)]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "Ruby 2.4.0",
+ "language": "ruby",
+ "name": "ruby"
+ },
+ "language_info": {
+ "file_extension": ".rb",
+ "mimetype": "application/x-ruby",
+ "name": "ruby",
+ "version": "2.4.0"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}