Done puzzle 58
authorNeil Smith <neil.git@njae.me.uk>
Wed, 12 Apr 2017 09:41:38 +0000 (10:41 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Wed, 12 Apr 2017 09:41:38 +0000 (10:41 +0100)
euler58.ipynb [new file with mode: 0644]

diff --git a/euler58.ipynb b/euler58.ipynb
new file mode 100644 (file)
index 0000000..29db832
--- /dev/null
@@ -0,0 +1,297 @@
+{
+ "cells": [
+  {
+   "cell_type": "markdown",
+   "metadata": {},
+   "source": [
+    "# Diagonals\n",
+    "\n",
+    "```\n",
+    "37 36 35 34 33 32 31\n",
+    "38 17 16 15 14 13 30\n",
+    "39 18  5  4  3 12 29\n",
+    "40 19  6  1  2 11 28\n",
+    "41 20  7  8  9 10 27\n",
+    "42 21 22 23 24 25 26\n",
+    "43 44 45 46 47 48 49\n",
+    "```\n",
+    "\n",
+    "Down-right goes as 1, 9, 25, 49: (2i+1)^2, starting from i=0\n",
+    "\n",
+    "Down left goes as 1 7 21 43: (2i+1)^2 - 2i\n",
+    "\n",
+    "Up left goes 1 5 17 37: (2i + 1)^2 - 4i\n",
+    "\n",
+    "Up right goes 1 3 13 31: (2i + 1)^2 - 6i\n"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 1,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "9973"
+      ]
+     },
+     "execution_count": 1,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "require_relative 'primes'\n",
+    "primes = Primes.instance\n",
+    "# primes.load_primes \"primes_2000000.txt\"\n",
+    "primes.primes.last"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 2,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 9, 25, 49, 81]"
+      ]
+     },
+     "execution_count": 2,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(0...5).map {|i| (2 * i + 1) ** 2}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 3,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 7, 21, 43, 73]"
+      ]
+     },
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(0...5).map {|i| (2 * i + 1) ** 2 - 2 * i}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 4,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 5, 17, 37, 65]"
+      ]
+     },
+     "execution_count": 4,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(0...5).map {|i| (2 * i + 1) ** 2 - 4 * i}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 3, 13, 31, 57]"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "(0...5).map {|i| (2 * i + 1) ** 2 - 6 * i}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 6,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       ":diagonals"
+      ]
+     },
+     "execution_count": 6,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "def diagonals(n)\n",
+    "  [1] + (1...n).flat_map {|i| [\n",
+    "      (2 * i + 1) ** 2,\n",
+    "      (2 * i + 1) ** 2 - 2 * i,\n",
+    "      (2 * i + 1) ** 2 - 4 * i,\n",
+    "      (2 * i + 1) ** 2 - 6 * i\n",
+    "      ]}\n",
+    "end"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 7,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       ":diagonal_tips"
+      ]
+     },
+     "execution_count": 7,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "def diagonal_tips(n)\n",
+    "  [# (2 * n + 1) ** 2,\n",
+    "   (2 * n + 1) ** 2 - 2 * n,\n",
+    "   (2 * n + 1) ** 2 - 4 * n,\n",
+    "   (2 * n + 1) ** 2 - 6 * n]\n",
+    "end"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 8,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[1, 9, 7, 5, 3, 25, 21, 17, 13]"
+      ]
+     },
+     "execution_count": 8,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "diagonals(3)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 9,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "9"
+      ]
+     },
+     "execution_count": 9,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "diagonals(3).length"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 10,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "[7, 5, 3, 17, 13]"
+      ]
+     },
+     "execution_count": 10,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "diagonals(3).select {|n| n.prime?}"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 11,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "26241"
+      ]
+     },
+     "execution_count": 11,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "l = 1\n",
+    "tip_primes = diagonal_tips(l).select {|n| primes.is_prime? n}\n",
+    "prime_count = tip_primes.length\n",
+    "while prime_count * 10 > (4 * l + 1)\n",
+    "  # puts l if l % 500 == 0\n",
+    "  l += 1\n",
+    "  tip_primes = diagonal_tips(l).select {|n| primes.is_prime? n}\n",
+    "  prime_count += tip_primes.length\n",
+    "end\n",
+    "l * 2 + 1 # side 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": 2
+}