Finished problem 39 and added some new ones.

main
Felix Martin 2018-05-20 16:32:19 -04:00
parent aa14d544d2
commit 894160d1a0
11 changed files with 35915 additions and 9 deletions

View File

@ -13,21 +13,116 @@
"For which value of p ≤ 1000, is the number of solutions maximised?"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"We are looking for right angle triangles so Pythagoras' theorem $a^2 + b^2 = c^2$ can be used. Also it must be true that $a <= b <= c$ is true for every solution. Let's start with a function that tests the given examples."
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def is_right_angle_triangle(a, b, c, p):\n",
" if a + b + c != p:\n",
" return False\n",
" if a**2 + b**2 != c**2:\n",
" return False\n",
" return True\n",
"\n",
"given = [(20, 48, 52, 120), (24, 45, 51, 120), (30, 40, 50, 120)]\n",
"for g in given:\n",
" assert(is_right_angle_triangle(*g))\n",
"assert(is_right_angle_triangle(29, 41, 50, 120) == False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This seems bruteforceable. Let's try to find the solutions for p = 120."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(30, 40, 50), (24, 45, 51), (20, 48, 52)]\n"
]
}
],
"source": [
"def brute_force(p):\n",
" solutions = [(a,b, p - a - b)\n",
" for b in range(1, p // 2 + 1)\n",
" for a in range(1, b)\n",
" if a*a + b*b == (p - a - b) * (p - a - b)\n",
" ]\n",
" return solutions\n",
"\n",
"print(brute_force(120))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Looks good. Let's try for all $p <= 1000$."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
"source": [
"solutions = [(len(brute_force(p)), p) for p in range(1, 1001)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Well, not too fast, but also not too slow. Let's get the max and we have our solution."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"840\n"
]
}
],
"source": [
"s, p = max(solutions)\n",
"print(p)\n",
"assert(p == 840)"
]
}
],
"metadata": {
"completion_date": "",
"completion_date": "Sun, 20 May 2018, 21:16",
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"language": "python3.6",
"name": "python3"
},
"language_info": {
@ -40,7 +135,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.5"
"version": "3.6.5"
},
"tags": [
"triangle",
@ -49,5 +144,5 @@
]
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}

View File

@ -0,0 +1,57 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pandigital prime (Euler Problem 41)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime.\n",
"\n",
"What is the largest n-digit pandigital prime that exists?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"tags": [
"pandigital",
"prime"
]
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,59 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Coded triangle numbers (Euler Problem 42)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The nth term of the sequence of triangle numbers is given by, $t_n = \\frac{1}{2}n(n+1)$; so the first ten triangle numbers are:\n",
"\n",
" 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...\n",
"\n",
"By converting each letter in a word to a number corresponding to its alphabetical position and adding these values we form a word value. For example, the word value for SKY is 19 + 11 + 25 = 55 = $t_{10}$. If the word value is a triangle number then we shall call the word a triangle word.\n",
"\n",
"Using words.txt (right click and 'Save Link/Target As...'), a 16K text file containing nearly two-thousand common English words, how many are triangle words? (The file is saved in the same directory as this notebook file as EulerProblem042.txt)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"tags": [
"triangle",
"words"
]
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,73 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Sub-string divisibility (Euler Problem 43)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"The number, 1406357289, is a 0 to 9 pandigital number because it is made up of each of the digits 0 to 9 in some order, but it also has a rather interesting sub-string divisibility property.\n",
"\n",
"Let $d_1$ be the 1st digit, $d_2$ be the 2nd digit, and so on. In this way, we note the following:\n",
"\n",
"$d_2d_3d_4$=406 is divisible by 2\n",
"\n",
"$d_3d_4d_5$=063 is divisible by 3\n",
"\n",
"$d_4d_5d_6$=635 is divisible by 5\n",
"\n",
"$d_5d_6d_7=357$ is divisible by 7\n",
"\n",
"$d_6d_7d_8=572$ is divisible by 11\n",
"\n",
"$d_7d_8d_9=728$ is divisible by 13\n",
"\n",
"$d_8d_9d_{10} =289$ is divisible by 17\n",
"\n",
"Find the sum of all 0 to 9 pandigital numbers with this property."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"tags": [
"pandigital",
"divisibility"
]
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -11779,21 +11779,152 @@ div#notebook {
<p>$\{20,48,52\}, \{24,45,51\}, \{30,40,50\}$</p>
<p>For which value of p ≤ 1000, is the number of solutions maximised?</p>
</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>We are looking for right angle triangles so Pythagoras' theorem $a^2 + b^2 = c^2$ can be used. Also it must be true that $a &lt;= b &lt;= c$ is true for every solution. Let's start with a function that tests the given examples.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">is_right_angle_triangle</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="n">c</span><span class="p">,</span> <span class="n">p</span><span class="p">):</span>
<span class="k">if</span> <span class="n">a</span> <span class="o">+</span> <span class="n">b</span> <span class="o">+</span> <span class="n">c</span> <span class="o">!=</span> <span class="n">p</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">if</span> <span class="n">a</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="n">b</span><span class="o">**</span><span class="mi">2</span> <span class="o">!=</span> <span class="n">c</span><span class="o">**</span><span class="mi">2</span><span class="p">:</span>
<span class="k">return</span> <span class="kc">False</span>
<span class="k">return</span> <span class="kc">True</span>
<span class="n">given</span> <span class="o">=</span> <span class="p">[(</span><span class="mi">20</span><span class="p">,</span> <span class="mi">48</span><span class="p">,</span> <span class="mi">52</span><span class="p">,</span> <span class="mi">120</span><span class="p">),</span> <span class="p">(</span><span class="mi">24</span><span class="p">,</span> <span class="mi">45</span><span class="p">,</span> <span class="mi">51</span><span class="p">,</span> <span class="mi">120</span><span class="p">),</span> <span class="p">(</span><span class="mi">30</span><span class="p">,</span> <span class="mi">40</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">120</span><span class="p">)]</span>
<span class="k">for</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">given</span><span class="p">:</span>
<span class="k">assert</span><span class="p">(</span><span class="n">is_right_angle_triangle</span><span class="p">(</span><span class="o">*</span><span class="n">g</span><span class="p">))</span>
<span class="k">assert</span><span class="p">(</span><span class="n">is_right_angle_triangle</span><span class="p">(</span><span class="mi">29</span><span class="p">,</span> <span class="mi">41</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span> <span class="o">==</span> <span class="kc">False</span><span class="p">)</span>
</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>This seems bruteforceable. Let's try to find the solutions for p = 120.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">brute_force</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
<span class="n">solutions</span> <span class="o">=</span> <span class="p">[(</span><span class="n">a</span><span class="p">,</span><span class="n">b</span><span class="p">,</span> <span class="n">p</span> <span class="o">-</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span>
<span class="k">for</span> <span class="n">b</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="n">p</span> <span class="o">//</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">for</span> <span class="n">a</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="n">b</span><span class="p">)</span>
<span class="k">if</span> <span class="n">a</span><span class="o">*</span><span class="n">a</span> <span class="o">+</span> <span class="n">b</span><span class="o">*</span><span class="n">b</span> <span class="o">==</span> <span class="p">(</span><span class="n">p</span> <span class="o">-</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span> <span class="o">*</span> <span class="p">(</span><span class="n">p</span> <span class="o">-</span> <span class="n">a</span> <span class="o">-</span> <span class="n">b</span><span class="p">)</span>
<span class="p">]</span>
<span class="k">return</span> <span class="n">solutions</span>
<span class="nb">print</span><span class="p">(</span><span class="n">brute_force</span><span class="p">(</span><span class="mi">120</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>[(30, 40, 50), (24, 45, 51), (20, 48, 52)]
</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>Looks good. Let's try for all $p &lt;= 1000$.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">solutions</span> <span class="o">=</span> <span class="p">[(</span><span class="nb">len</span><span class="p">(</span><span class="n">brute_force</span><span class="p">(</span><span class="n">p</span><span class="p">)),</span> <span class="n">p</span><span class="p">)</span> <span class="k">for</span> <span class="n">p</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">1001</span><span class="p">)]</span>
</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>Well, not too fast, but also not too slow. Let's get the max and we have our solution.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">s</span><span class="p">,</span> <span class="n">p</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="n">solutions</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">p</span><span class="p">)</span>
<span class="k">assert</span><span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="mi">840</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>840
</pre>
</div>
</div>
</div>
</div>
</div>
</div>
</div>

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -576,7 +576,7 @@
<tr>
<td>Problem 039</td>
<td></td>
<td>Sun, 20 May 2018, 21:16</td>
<td><a href="EulerProblem039.html">Problem 039</a></td>
<td>
@ -604,6 +604,45 @@
</td>
</tr>
<tr>
<td>Problem 041</td>
<td></td>
<td><a href="EulerProblem041.html">Problem 041</a></td>
<td>
<kbd>pandigital</kbd>
<kbd>prime</kbd>
</td>
</tr>
<tr>
<td>Problem 042</td>
<td></td>
<td><a href="EulerProblem042.html">Problem 042</a></td>
<td>
<kbd>triangle</kbd>
<kbd>words</kbd>
</td>
</tr>
<tr>
<td>Problem 043</td>
<td></td>
<td><a href="EulerProblem043.html">Problem 043</a></td>
<td>
<kbd>pandigital</kbd>
<kbd>divisibility</kbd>
</td>
</tr>
<tr>
<td>Problem 067</td>
<td>Fri, 5 Sep 2014, 07:36</td>

2
ipython/publish.py Normal file → Executable file
View File

@ -1,3 +1,5 @@
#!/usr/bin/env python
import jinja2
import os
import subprocess