Did 28 in ipython.
This commit is contained in:
@@ -27,39 +27,87 @@
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"I would try to create a function $f(n)$ which yields the sum of the outmost ring of a n by n spiral.\n",
|
||||
"\n",
|
||||
"For example:\n",
|
||||
"\n",
|
||||
"$f(1) = 1$\n",
|
||||
"\n",
|
||||
"$f(3) = 3 + 5 + 7 + 9 = 24$\n",
|
||||
"\n",
|
||||
"$f(5) = 13 + 17 + 21 + 25 = 76$\n",
|
||||
"\n",
|
||||
"When we have this function we calculate the solution simply by\n",
|
||||
"\n",
|
||||
"~~~\n",
|
||||
"s = sum([f(n) for n in range(1, 1002, 2)])\n",
|
||||
"~~~\n",
|
||||
"\n",
|
||||
"For each outer ring there is an initial corner value c ($c_3 = 3, c_5 = 76$). Once we have this value we can caluclate f like $f(n) = c_{n} + (c_n + n - 1) + (c_n + 2(n-1)) + (c_n + 3(n-1)) = 4c_n + 6 (n-1)$"
|
||||
"When we know the corner of a certain spiral we can calculate it's total like $f_n = 4 c_n + 6 (n - 1)$. We then only have to update the corner value for each spiral. "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 1,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def f(n):\n",
|
||||
" if n == 1:\n",
|
||||
" return 1\n",
|
||||
" return 0\n",
|
||||
"total = 1\n",
|
||||
"current_corner = 3\n",
|
||||
"\n",
|
||||
"s = sum([f(n) for n in range(1, 1002, 2)])\n",
|
||||
"for n in range(3, 1002, 2):\n",
|
||||
" total += 4 * current_corner + 6 * (n - 1)\n",
|
||||
" current_corner += 4 * n - 2\n",
|
||||
"\n",
|
||||
"s = total"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"669171001"
|
||||
]
|
||||
},
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"assert(s == 669171001)\n",
|
||||
"s"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"The only missing piece is how could we calculate the current corner value for a certain n. The series for this is as follows:\n",
|
||||
"\n",
|
||||
"$c = 1, 3, 13, 31, 57, 91, 133, 183, 241, \\dots$\n",
|
||||
"\n",
|
||||
"With some experimenting it can be seen that\n",
|
||||
"\n",
|
||||
"$c_n = (n - 1)^2 - (n - 2) = n^2 - 2n + 1 - n + 2 = n^2 - 3n + 3$.\n",
|
||||
"\n",
|
||||
"Now, we can insert $c_n$ into $f_n$ which gives as the sum of corners for the nth spiral:\n",
|
||||
"\n",
|
||||
"$f_n = 4n^2 - 12n + 12 + 6n - 6 = 4n^2 - 6n + 6$"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
"outputs": [
|
||||
{
|
||||
"data": {
|
||||
"text/plain": [
|
||||
"669171001"
|
||||
]
|
||||
},
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"output_type": "execute_result"
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"s = 1 + sum([4 * n * n - 6 * n + 6 for n in range(3, 1002, 2)])\n",
|
||||
"assert(s == 669171001)\n",
|
||||
"s"
|
||||
]
|
||||
@@ -82,7 +130,7 @@
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.3"
|
||||
"version": "3.5.4"
|
||||
},
|
||||
"tags": [
|
||||
"spiral",
|
||||
|
||||
Reference in New Issue
Block a user