--- /dev/null
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:2513cb86760ebf975b3dce5f7884ee5e23c13f94e20cc9af8fc245f251455fda"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Project Euler problem 11\n",
+ "Find the largest product of four adjacent numbers in the grid."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Constants"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ROWS = COLUMNS = 20\n",
+ "SECTION_LEN = 4"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 97
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Data structure\n",
+ "Convert the text of the numbers into a 2d array of integers.\n",
+ "\n",
+ "(Alterntive data structures include a 1d list of integers, or a dict with keys of (r, c) pairs.)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "GRID_STRING = \"\"\"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n",
+ "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n",
+ "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n",
+ "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n",
+ "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n",
+ "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n",
+ "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n",
+ "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n",
+ "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n",
+ "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n",
+ "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n",
+ "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n",
+ "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n",
+ "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n",
+ "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n",
+ "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n",
+ "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n",
+ "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n",
+ "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n",
+ "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48\"\"\"\n",
+ "\n",
+ "GRID_LIST = [int(n) for n in GRID_STRING.split()]\n",
+ "GRID = [GRID_LIST[i:i+COLUMNS] for i in range(0, ROWS * COLUMNS, COLUMNS)]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 98
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for row in GRID:\n",
+ " print(row)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "[8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8]\n",
+ "[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0]\n",
+ "[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65]\n",
+ "[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91]\n",
+ "[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80]\n",
+ "[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50]\n",
+ "[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70]\n",
+ "[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21]\n",
+ "[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72]\n",
+ "[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95]\n",
+ "[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92]\n",
+ "[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57]\n",
+ "[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58]\n",
+ "[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40]\n",
+ "[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66]\n",
+ "[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69]\n",
+ "[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36]\n",
+ "[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16]\n",
+ "[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54]\n",
+ "[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]\n"
+ ]
+ }
+ ],
+ "prompt_number": 99
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#Directions\n",
+ "What lines do we examine? Each number paricipates in up to 8 \u00d7 4 = 32 lines (fewer near the edges), but we can use the fact that multipication is commutative to only examine four lines that start at a number.\n",
+ "\n",
+ "`directions` stores those directions, and how to move in the direction."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Directions, as the pair (difference-in-row, difference-in-column)\n",
+ "DIRECTIONS = {'N': (-1, 0), 'NW': (-1, -1), 'W': (0, -1), 'SW': (1, -1)}"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 20
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Finding the right numbers\n",
+ "Given a starting position and a direction, find the right numbers.\n",
+ "\n",
+ "### Question:\n",
+ "Should we worry if the request goes out of the bounds of the grid?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def numbers(row, column, direction):\n",
+ " nums = []\n",
+ " dr, dc = DIRECTIONS[direction]\n",
+ " for _ in range(SECTION_LEN):\n",
+ " nums.append(GRID[row][column])\n",
+ " row += dr\n",
+ " column += dc\n",
+ " return nums"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 71
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Test it."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "numbers(0, 3, 'W')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 72,
+ "text": [
+ "[97, 22, 2, 8]"
+ ]
+ }
+ ],
+ "prompt_number": 72
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "numbers(3, 0, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 73,
+ "text": [
+ "[52, 81, 49, 8]"
+ ]
+ }
+ ],
+ "prompt_number": 73
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "numbers(3, 3, 'NW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 74,
+ "text": [
+ "[23, 31, 49, 8]"
+ ]
+ }
+ ],
+ "prompt_number": 74
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "numbers(3, 3, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 75,
+ "text": [
+ "[23, 16, 47, 32]"
+ ]
+ }
+ ],
+ "prompt_number": 75
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Product of a list"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def product(ns):\n",
+ " p = 1\n",
+ " for n in ns:\n",
+ " p *= n\n",
+ " return p"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 25
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "product(numbers(0, 3, 'W'))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 26,
+ "text": [
+ "34144"
+ ]
+ }
+ ],
+ "prompt_number": 26
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "97 * 22 * 2 * 8"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 27,
+ "text": [
+ "34144"
+ ]
+ }
+ ],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## What directions don't take us out outside of the boundaries?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def valid_direction_explicit(row, column, direction):\n",
+ " if direction == 'N' and row >= SECTION_LEN -1:\n",
+ " return True\n",
+ " elif direction == 'W' and column >= SECTION_LEN -1:\n",
+ " return True\n",
+ " elif direction == 'NW' and row >= SECTION_LEN -1 and column >= SECTION_LEN -1:\n",
+ " return True\n",
+ " elif direction == 'SW' and row + SECTION_LEN <= ROWS and column >= SECTION_LEN -1:\n",
+ " return True\n",
+ " else:\n",
+ " return False"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 63
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(0, 0, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 33,
+ "text": [
+ "False"
+ ]
+ }
+ ],
+ "prompt_number": 33
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(5, 5, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 35,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 35
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(5, 5, 'NW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 36,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 36
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(5, 5, 'W')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 37,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 37
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(5, 5, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 38,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 38
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(17, 5, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 61,
+ "text": [
+ "False"
+ ]
+ }
+ ],
+ "prompt_number": 61
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(16, 5, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 64,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 64
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction_explicit(2, 2, 'NW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 60,
+ "text": [
+ "False"
+ ]
+ }
+ ],
+ "prompt_number": 60
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def valid_direction(row, column, direction):\n",
+ " dr, dc = DIRECTIONS[direction]\n",
+ " end_row = row + dr * (SECTION_LEN -1)\n",
+ " end_col = column + dc * (SECTION_LEN -1)\n",
+ " if end_row >= 0 and end_row < ROWS and end_col >= 0 and end_col < COLUMNS:\n",
+ " return True\n",
+ " else:\n",
+ " return False"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 79
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(0, 0, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 80,
+ "text": [
+ "False"
+ ]
+ }
+ ],
+ "prompt_number": 80
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(3, 3, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 81,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 81
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(3, 3, 'NW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 82,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 82
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(3, 3, 'W')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 83,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 83
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(3, 3, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 84,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 84
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(17, 17, 'N')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 85,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 85
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(17, 17, 'NW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 86,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 86
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(17, 17, 'W')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 87,
+ "text": [
+ "True"
+ ]
+ }
+ ],
+ "prompt_number": 87
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "valid_direction(17, 17, 'SW')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 89,
+ "text": [
+ "False"
+ ]
+ }
+ ],
+ "prompt_number": 89
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Now to solve the problem"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "best_product = 0\n",
+ "for row in range(ROWS):\n",
+ " for column in range(COLUMNS):\n",
+ " for direction in DIRECTIONS:\n",
+ " if valid_direction(row, column, direction):\n",
+ " this_product = product(numbers(row, column, direction))\n",
+ " if this_product > best_product:\n",
+ " best_product = this_product\n",
+ "best_product"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 94,
+ "text": [
+ "70600674"
+ ]
+ }
+ ],
+ "prompt_number": 94
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "max(product(numbers(r, c, d)) \n",
+ " for r in range(ROWS) \n",
+ " for c in range(COLUMNS) \n",
+ " for d in DIRECTIONS \n",
+ " if valid_direction(r, c, d))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 101,
+ "text": [
+ "70600674"
+ ]
+ }
+ ],
+ "prompt_number": 101
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# All the code in one place"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "ROWS = COLUMNS = 20\n",
+ "SECTION_LEN = 4\n",
+ "\n",
+ "GRID_STRING = \"\"\"08 02 22 97 38 15 00 40 00 75 04 05 07 78 52 12 50 77 91 08\n",
+ "49 49 99 40 17 81 18 57 60 87 17 40 98 43 69 48 04 56 62 00\n",
+ "81 49 31 73 55 79 14 29 93 71 40 67 53 88 30 03 49 13 36 65\n",
+ "52 70 95 23 04 60 11 42 69 24 68 56 01 32 56 71 37 02 36 91\n",
+ "22 31 16 71 51 67 63 89 41 92 36 54 22 40 40 28 66 33 13 80\n",
+ "24 47 32 60 99 03 45 02 44 75 33 53 78 36 84 20 35 17 12 50\n",
+ "32 98 81 28 64 23 67 10 26 38 40 67 59 54 70 66 18 38 64 70\n",
+ "67 26 20 68 02 62 12 20 95 63 94 39 63 08 40 91 66 49 94 21\n",
+ "24 55 58 05 66 73 99 26 97 17 78 78 96 83 14 88 34 89 63 72\n",
+ "21 36 23 09 75 00 76 44 20 45 35 14 00 61 33 97 34 31 33 95\n",
+ "78 17 53 28 22 75 31 67 15 94 03 80 04 62 16 14 09 53 56 92\n",
+ "16 39 05 42 96 35 31 47 55 58 88 24 00 17 54 24 36 29 85 57\n",
+ "86 56 00 48 35 71 89 07 05 44 44 37 44 60 21 58 51 54 17 58\n",
+ "19 80 81 68 05 94 47 69 28 73 92 13 86 52 17 77 04 89 55 40\n",
+ "04 52 08 83 97 35 99 16 07 97 57 32 16 26 26 79 33 27 98 66\n",
+ "88 36 68 87 57 62 20 72 03 46 33 67 46 55 12 32 63 93 53 69\n",
+ "04 42 16 73 38 25 39 11 24 94 72 18 08 46 29 32 40 62 76 36\n",
+ "20 69 36 41 72 30 23 88 34 62 99 69 82 67 59 85 74 04 36 16\n",
+ "20 73 35 29 78 31 90 01 74 31 49 71 48 86 81 16 23 57 05 54\n",
+ "01 70 54 71 83 51 54 69 16 92 33 48 61 43 52 01 89 19 67 48\"\"\"\n",
+ "\n",
+ "GRID_LIST = [int(n) for n in GRID_STRING.split()]\n",
+ "GRID = [GRID_LIST[i:i+COLUMNS] for i in range(0, ROWS * COLUMNS, COLUMNS)]\n",
+ "\n",
+ "# Directions, as the pair (difference-in-row, difference-in-column)\n",
+ "DIRECTIONS = {'N': (-1, 0), 'NW': (-1, -1), 'W': (0, -1), 'SW': (1, -1)}\n",
+ "\n",
+ "def numbers(row, column, direction):\n",
+ " nums = []\n",
+ " dr, dc = DIRECTIONS[direction]\n",
+ " for _ in range(SECTION_LEN):\n",
+ " nums.append(GRID[row][column])\n",
+ " row += dr\n",
+ " column += dc\n",
+ " return nums\n",
+ "\n",
+ "def product(ns):\n",
+ " p = 1\n",
+ " for n in ns:\n",
+ " p *= n\n",
+ " return p\n",
+ "\n",
+ "def valid_direction(row, column, direction):\n",
+ " dr, dc = DIRECTIONS[direction]\n",
+ " end_row = row + dr * (SECTION_LEN -1)\n",
+ " end_col = column + dc * (SECTION_LEN -1)\n",
+ " if end_row >= 0 and end_row < ROWS and end_col >= 0 and end_col < COLUMNS:\n",
+ " return True\n",
+ " else:\n",
+ " return False\n",
+ "\n",
+ "max(product(numbers(r, c, d)) \n",
+ " for r in range(ROWS) \n",
+ " for c in range(COLUMNS) \n",
+ " for d in DIRECTIONS \n",
+ " if valid_direction(r, c, d)) "
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 102,
+ "text": [
+ "70600674"
+ ]
+ }
+ ],
+ "prompt_number": 102
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": []
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file
--- /dev/null
+{
+ "metadata": {
+ "name": "",
+ "signature": "sha256:910fd3bd845f28d453d531005afb4e6d3ce4f040aa35835ad49dbb76f9e611fa"
+ },
+ "nbformat": 3,
+ "nbformat_minor": 0,
+ "worksheets": [
+ {
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Euler 14\n",
+ "\n",
+ "Which starting number, under one million, produces the longest Collatz chain?"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# We'll need this in a bit\n",
+ "import sys\n",
+ "sys.setrecursionlimit(1000000)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 79
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "This is the obvious implementation of the problem: the length of the chain is one more than the length of the chain from the next number. \n",
+ "\n",
+ "For instance, given the chain:\n",
+ "\n",
+ " 13 \u2192 40 \u2192 20 \u2192 10 \u2192 5 \u2192 16 \u2192 8 \u2192 4 \u2192 2 \u2192 1\n",
+ "\n",
+ "we know that the length of chain starting at 13 is one more than the length of chain starting at 40, which is one more than the length of the chain starting at 20, ...\n",
+ "\n",
+ "Notice that we don't care about the values found in the chain, simply how long it is."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def collatz_length(n):\n",
+ " if n == 1:\n",
+ " return 1\n",
+ " elif n % 2 == 0:\n",
+ " return 1 + collatz_length(n // 2)\n",
+ " else:\n",
+ " return 1 + collatz_length(3 * n + 1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 80
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Test it works"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_length(1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 81,
+ "text": [
+ "1"
+ ]
+ }
+ ],
+ "prompt_number": 81
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_length(2)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 82,
+ "text": [
+ "2"
+ ]
+ }
+ ],
+ "prompt_number": 82
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_length(3)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 83,
+ "text": [
+ "8"
+ ]
+ }
+ ],
+ "prompt_number": 83
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_length(13)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 84,
+ "text": [
+ "10"
+ ]
+ }
+ ],
+ "prompt_number": 84
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "for i in range(1, 10):\n",
+ " print(i, collatz_length(i))"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "1 1\n",
+ "2 2\n",
+ "3 8\n",
+ "4 3\n",
+ "5 6\n",
+ "6 9\n",
+ "7 17\n",
+ "8 4\n",
+ "9 20\n"
+ ]
+ }
+ ],
+ "prompt_number": 85
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Performance\n",
+ "Let's find the longest chain starting from a number <= 10,000. We'll time how long it takes."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "longest_start = 1\n",
+ "longest_chain = 0\n",
+ "for i in range(1, 10001):\n",
+ " this_chain = collatz_length(i)\n",
+ " if this_chain > longest_chain:\n",
+ " longest_start = i\n",
+ " longest_chain = this_chain\n",
+ "print(longest_start, '->', longest_chain)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "1 loops, best of 3: 188 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 86
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "Now try up to 1,000,000."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "longest_start = 1\n",
+ "longest_chain = 0\n",
+ "for i in range(1, 1000001):\n",
+ " this_chain = collatz_length(i)\n",
+ " if this_chain > longest_chain:\n",
+ " longest_start = i\n",
+ " longest_chain = this_chain\n",
+ "print(longest_start, '->', longest_chain)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "837799 -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "1 loops, best of 3: 28.4 s per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 87
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Better performance\n",
+ "This is slow. Can we do better?\n",
+ "\n",
+ "Recall the sequence starting at 13:\n",
+ "\n",
+ "13 \u2192 40 \u2192 20 \u2192 10 \u2192 5 \u2192 16 \u2192 8 \u2192 4 \u2192 2 \u2192 1\n",
+ "\n",
+ "If we're finding the Collatz chain lengths for all numbers up to 13, we've just calculated the Collatz chain length of 10. We don't need to recalculate it. \n",
+ "\n",
+ "Instead, store the results in a cache and look them up if we have them."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_cache = {}\n",
+ "\n",
+ "def collatz_length_cache(n):\n",
+ " if n not in collatz_cache:\n",
+ " if n == 1:\n",
+ " collatz_cache[n] = 1\n",
+ " elif n % 2 == 0:\n",
+ " collatz_cache[n] = 1 + collatz_length_cache(n // 2)\n",
+ " else:\n",
+ " collatz_cache[n] = 1 + collatz_length_cache(3 * n + 1)\n",
+ " return collatz_cache[n]"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 88
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_length_cache(9)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 89,
+ "text": [
+ "20"
+ ]
+ }
+ ],
+ "prompt_number": 89
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "collatz_cache"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 90,
+ "text": [
+ "{1: 1,\n",
+ " 2: 2,\n",
+ " 34: 14,\n",
+ " 4: 3,\n",
+ " 5: 6,\n",
+ " 17: 13,\n",
+ " 40: 9,\n",
+ " 9: 20,\n",
+ " 10: 7,\n",
+ " 11: 15,\n",
+ " 13: 10,\n",
+ " 14: 18,\n",
+ " 16: 5,\n",
+ " 8: 4,\n",
+ " 20: 8,\n",
+ " 22: 16,\n",
+ " 7: 17,\n",
+ " 52: 12,\n",
+ " 26: 11,\n",
+ " 28: 19}"
+ ]
+ }
+ ],
+ "prompt_number": 90
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "longest_start = 1\n",
+ "longest_chain = 0\n",
+ "collatz_cache = {}\n",
+ "for i in range(1, 10001):\n",
+ " this_chain = collatz_length_cache(i)\n",
+ " if this_chain > longest_chain:\n",
+ " longest_start = i\n",
+ " longest_chain = this_chain\n",
+ "print(longest_start, '->', longest_chain)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "6171 -> 262\n",
+ "100 loops, best of 3: 2.03 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 93
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "longest_start = 1\n",
+ "longest_chain = 0\n",
+ "collatz_cache = {}\n",
+ "for i in range(1, 1000001):\n",
+ " this_chain = collatz_length_cache(i)\n",
+ " if this_chain > longest_chain:\n",
+ " longest_start = i\n",
+ " longest_chain = this_chain\n",
+ "print(longest_start, '->', longest_chain)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "837799 -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "1 loops, best of 3: 248 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 94
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "#It's so useful, it's built in\n",
+ "Python 3.3 includes the `lru_cache` function decorator in the standard `functools` library. We can use that without faffing around with explicit caches."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "# Python 3.3 or higher:\n",
+ "from functools import lru_cache\n",
+ "\n",
+ "@lru_cache(maxsize=None)\n",
+ "def collatz_length_lru(n):\n",
+ " if n == 1:\n",
+ " return 1\n",
+ " elif n % 2 == 0:\n",
+ " return 1 + collatz_length_lru(n // 2)\n",
+ " else:\n",
+ " return 1 + collatz_length_lru(3 * n + 1)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 95
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "%%timeit\n",
+ "longest_start = 1\n",
+ "longest_chain = 0\n",
+ "for i in range(1, 1000001):\n",
+ " this_chain = collatz_length_lru(i)\n",
+ " if this_chain > longest_chain:\n",
+ " longest_start = i\n",
+ " longest_chain = this_chain\n",
+ "print(longest_start, '->', longest_chain)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ "837799 -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "837799"
+ ]
+ },
+ {
+ "output_type": "stream",
+ "stream": "stdout",
+ "text": [
+ " -> 525\n",
+ "1 loops, best of 3: 851 ms per loop\n"
+ ]
+ }
+ ],
+ "prompt_number": 96
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 96
+ }
+ ],
+ "metadata": {}
+ }
+ ]
+}
\ No newline at end of file