{
 "metadata": {
  "name": "euler44-end-jep"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from IPython.display import Latex"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 1
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def p(n):\n",
      "    return n * (3 * n - 1) / 2"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 2
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "# P(n) = n(3n-1)/2\n",
      "\n",
      "Latex(r'\\begin{align}'\n",
      "      r'P(n) &= n(3n-1)/2\\\\'\n",
      "      r'&= (3n^2-n)/2'\n",
      "      r'\\end{align}'\n",
      "      r'\\begin{align}'\n",
      "      r'P(n+d) &= (n+d)(3(n+d)-1)/2\\\\'\n",
      "      r'&= (n+d)(3n+3d-1)/2\\\\'\n",
      "      r'&= (3n^2+3nd-n+3nd+3d^2-d)/2\\\\'\n",
      "      r'&= (3n^2+6nd-n+3d^2-d)/2'\n",
      "      r'\\end{align}'\n",
      "      r'\\begin{align}'\n",
      "      r'P(n+d) - P(n) &= (3n^2+6nd-n+3d^2-d)/2-(3n^2-n)/2\\\\'\n",
      "      r'&= (6nd+3d^2-d)/2\\\\'\n",
      "      r'&= (6n+3d-1)d/2\\\\'\n",
      "      r'&= \\frac{(6n+3d-1)d}{2}'\n",
      "      r'\\end{align}'\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "\\begin{align}P(n) &= n(3n-1)/2\\\\&= (3n^2-n)/2\\end{align}\\begin{align}P(n+d) &= (n+d)(3(n+d)-1)/2\\\\&= (n+d)(3n+3d-1)/2\\\\&= (3n^2+3nd-n+3nd+3d^2-d)/2\\\\&= (3n^2+6nd-n+3d^2-d)/2\\end{align}\\begin{align}P(n+d) - P(n) &= (3n^2+6nd-n+3d^2-d)/2-(3n^2-n)/2\\\\&= (6nd+3d^2-d)/2\\\\&= (6n+3d-1)d/2\\\\&= \\frac{(6n+3d-1)d}{2}\\end{align}"
       ],
       "output_type": "pyout",
       "prompt_number": 3,
       "text": [
        "<IPython.core.display.Latex at 0xa0e0c0c>"
       ]
      }
     ],
     "prompt_number": 3
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "Latex(r'Given P(n+d) - P(n) and d, solve for n.'\n",
      "      r'\\begin{align}'\n",
      "      r'P(n+d) - P(n) &= \\frac{(6n+3d-1)\\cdot d}{2}\\\\'\n",
      "      r'\\frac{(6n+3d-1)\\cdot d}{2} &= P(n+d) - P(n)\\\\'\n",
      "      r'(6n+3d-1) &= 2\\frac{P(n+d) - P(n)}{d}\\\\'\n",
      "      r'6n &= 2\\frac{P(n+d) - P(n)}{d}-3d+1\\\\'\n",
      "      r'n &= \\frac{2\\frac{P(n+d) - P(n)}{d}-3d+1}{6}\\\\'\n",
      "      r'n+d &= \\frac{2\\frac{P(n+d) - P(n)}{d}-3d+1}{6}+d\\\\'\n",
      "      r'&= \\frac{2\\frac{P(n+d) - P(n)}{d}+3d+1}{6}'\n",
      "      r'\\end{align}'\n",
      ")"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "latex": [
        "Given P(n+d) - P(n) and d, solve for n.\\begin{align}P(n+d) - P(n) &= \\frac{(6n+3d-1)\\cdot d}{2}\\\\\\frac{(6n+3d-1)\\cdot d}{2} &= P(n+d) - P(n)\\\\(6n+3d-1) &= 2\\frac{P(n+d) - P(n)}{d}\\\\6n &= 2\\frac{P(n+d) - P(n)}{d}-3d+1\\\\n &= \\frac{2\\frac{P(n+d) - P(n)}{d}-3d+1}{6}\\\\n+d &= \\frac{2\\frac{P(n+d) - P(n)}{d}-3d+1}{6}+d\\\\&= \\frac{2\\frac{P(n+d) - P(n)}{d}+3d+1}{6}\\end{align}"
       ],
       "output_type": "pyout",
       "prompt_number": 4,
       "text": [
        "<IPython.core.display.Latex at 0xa0e942c>"
       ]
      }
     ],
     "prompt_number": 4
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "def n_plus_d_from_d_and_diff(d, diff):\n",
      "    return (2 * float(diff) / d + 3 * d + 1) / 6"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [],
     "prompt_number": 5
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "%timeit n_plus_d_from_d_and_diff(1, 5482660)\n",
      "n_plus_d_from_d_and_diff(1, 5482660)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "1000000 loops, best of 3: 1.05 us per loop\n"
       ]
      },
      {
       "output_type": "pyout",
       "prompt_number": 6,
       "text": [
        "1827554.0"
       ]
      }
     ],
     "prompt_number": 6
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Since P(n+d) - P(n) strictly increases for n, \n",
      "and P(n+d) - P(n) = 5482660 for n+d = 1827554 and d = 1, \n",
      "then n+d = 1827554 is the last value for j one needs to \n",
      "check for solutions that are as good as the first one. \n",
      "1827553 is the last value for j one needs to \n",
      "check for solutions that are better than the first one. \n"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}
