Done puzzle 44
authorNeil Smith <neil.git@njae.me.uk>
Fri, 17 Mar 2017 09:00:35 +0000 (09:00 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Fri, 17 Mar 2017 09:00:35 +0000 (09:00 +0000)
euler44.ipynb

index df13e128de9f7fde4df1c3035a463fb342ca04f7..2762a8417f1688162c895faaccd60d2d79723fd8 100644 (file)
@@ -2,7 +2,7 @@
  "cells": [
   {
    "cell_type": "code",
  "cells": [
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 2,
    "metadata": {
     "collapsed": false
    },
    "metadata": {
     "collapsed": false
    },
@@ -13,7 +13,7 @@
        ":pentagonal"
       ]
      },
        ":pentagonal"
       ]
      },
-     "execution_count": 1,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
      "metadata": {},
      "output_type": "execute_result"
     }
     "end"
    ]
   },
     "end"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "name": "stdout",
+     "output_type": "stream",
+     "text": [
+      "2167, 1020 -> 5482660\n",
+      "2167, 1020 -> 5482660\n"
+     ]
+    }
+   ],
+   "source": [
+    "pentagonals = Set.new()\n",
+    "\n",
+    "found = false\n",
+    "sum_index = 1\n",
+    "best_k = 0\n",
+    "best_j = 0\n",
+    "best_diff = pentagonal(5000)\n",
+    "\n",
+    "while !found do\n",
+    "  pentagonals << pentagonal(sum_index)\n",
+    "  ((sum_index/2)...sum_index).each do |j|\n",
+    "    k = sum_index - j\n",
+    "    diff = pentagonal(j) - pentagonal(k)\n",
+    "    if pentagonals.include?(diff) && pentagonals.include?(pentagonal(j)+pentagonal(k))\n",
+    "      found = true\n",
+    "      puts \"#{j}, #{k} -> #{diff}\"\n",
+    "      if diff < best_diff\n",
+    "        best_diff = diff\n",
+    "        best_k = k\n",
+    "        best_j = j\n",
+    "      end\n",
+    "    end\n",
+    "  end\n",
+    "  sum_index += 1\n",
+    "end\n",
+    "puts \"#{best_j}, #{best_k} -> #{best_diff}\""
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 18,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "true"
+      ]
+     },
+     "execution_count": 18,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pentagonals.include?(pentagonal(2167))"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {
+    "collapsed": false
+   },
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "210"
+      ]
+     },
+     "execution_count": 23,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "pentagonal(12)"
+   ]
+  },
   {
    "cell_type": "code",
    "execution_count": null,
   {
    "cell_type": "code",
    "execution_count": null,