Did 28 in ipython.

This commit is contained in:
Felix Martin 2018-02-12 00:08:20 +01:00
parent 21805e630c
commit bf8af81715
2 changed files with 156 additions and 40 deletions

View File

@ -202,30 +202,82 @@ div#notebook {
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<p>I would try to create a function $f(n)$ which yields the sum of the outmost ring of a n by n spiral.</p> <p>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.</p>
<p>For example:</p>
<p>$f(1) = 1$</p>
<p>$f(3) = 3 + 5 + 7 + 9 = 24$</p>
<p>$f(5) = 13 + 17 + 21 + 25 = 76$</p>
<p>When we have this function we calculate the solution simply by</p>
<pre><code>s = sum([f(n) for n in range(1, 1002, 2)])</code></pre>
<p>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)$</p>
</div> </div>
</div> </div>
</div> </div>
<div class="cell border-box-sizing code_cell rendered"> <div class="cell border-box-sizing code_cell rendered">
<div class="input"> <div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div> <div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell"> <div class="inner_cell">
<div class="input_area"> <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">n</span><span class="p">):</span> <div class=" highlight hl-ipython3"><pre><span></span><span class="n">total</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span> <span class="n">current_corner</span> <span class="o">=</span> <span class="mi">3</span>
<span class="k">return</span> <span class="mi">1</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="n">s</span> <span class="o">=</span> <span class="nb">sum</span><span class="p">([</span><span class="n">f</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1002</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1002</span><span class="p">,</span> <span class="mi">2</span><span class="p">):</span>
<span class="n">total</span> <span class="o">+=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">current_corner</span> <span class="o">+</span> <span class="mi">6</span> <span class="o">*</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span>
<span class="n">current_corner</span> <span class="o">+=</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">2</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">total</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">assert</span><span class="p">(</span><span class="n">s</span> <span class="o">==</span> <span class="mi">669171001</span><span class="p">)</span>
<span class="n">s</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[2]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>669171001</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>The only missing piece is how could we calculate the current corner value for a certain n. The series for this is as follows:</p>
<p>$c = 1, 3, 13, 31, 57, 91, 133, 183, 241, \dots$</p>
<p>With some experimenting it can be seen that</p>
<p>$c_n = (n - 1)^2 - (n - 2) = n^2 - 2n + 1 - n + 2 = n^2 - 3n + 3$.</p>
<p>Now, we can insert $c_n$ into $f_n$ which gives as the sum of corners for the nth spiral:</p>
<p>$f_n = 4n^2 - 12n + 12 + 6n - 6 = 4n^2 - 6n + 6$</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">s</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="nb">sum</span><span class="p">([</span><span class="mi">4</span> <span class="o">*</span> <span class="n">n</span> <span class="o">*</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">6</span> <span class="o">*</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">6</span> <span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">1002</span><span class="p">,</span> <span class="mi">2</span><span class="p">)])</span>
<span class="k">assert</span><span class="p">(</span><span class="n">s</span> <span class="o">==</span> <span class="mi">669171001</span><span class="p">)</span> <span class="k">assert</span><span class="p">(</span><span class="n">s</span> <span class="o">==</span> <span class="mi">669171001</span><span class="p">)</span>
<span class="n">s</span> <span class="n">s</span>
</pre></div> </pre></div>
@ -234,6 +286,22 @@ div#notebook {
</div> </div>
</div> </div>
<div class="output_wrapper">
<div class="output">
<div class="output_area"><div class="prompt output_prompt">Out[3]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>669171001</pre>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View File

@ -27,39 +27,87 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "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", "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. "
"\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)$"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": { "metadata": {
"collapsed": true "collapsed": false
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"def f(n):\n", "total = 1\n",
" if n == 1:\n", "current_corner = 3\n",
" return 1\n",
" return 0\n",
"\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", "assert(s == 669171001)\n",
"s" "s"
] ]
@ -82,7 +130,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.3" "version": "3.5.4"
}, },
"tags": [ "tags": [
"spiral", "spiral",