Finished 33 and 34.
This commit is contained in:
353
ipython/EulerProblem033.html
Normal file
353
ipython/EulerProblem033.html
Normal file
File diff suppressed because one or more lines are too long
172
ipython/EulerProblem033.ipynb
Normal file
172
ipython/EulerProblem033.ipynb
Normal file
@@ -0,0 +1,172 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 33\n",
|
||||||
|
"\n",
|
||||||
|
"The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.\n",
|
||||||
|
"\n",
|
||||||
|
"We shall consider fractions like, 30/50 = 3/5, to be trivial examples.\n",
|
||||||
|
"\n",
|
||||||
|
"There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.\n",
|
||||||
|
"\n",
|
||||||
|
"If the product of these four fractions is given in its lowest common terms, find the value of the denominator."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"We start write a function which checks if a number is curios and then brute force."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 21,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"def is_curious(n, d):\n",
|
||||||
|
" assert(len(str(n)) == 2 and len(str(d)) == 2)\n",
|
||||||
|
" if n == d:\n",
|
||||||
|
" return False\n",
|
||||||
|
" for i in range(1, 10):\n",
|
||||||
|
" if str(i) in str(n) and str(i) in str(d):\n",
|
||||||
|
" try:\n",
|
||||||
|
" n_ = int(str(n).replace(str(i), \"\"))\n",
|
||||||
|
" d_ = int(str(d).replace(str(i), \"\"))\n",
|
||||||
|
" except ValueError:\n",
|
||||||
|
" return False\n",
|
||||||
|
" try:\n",
|
||||||
|
" if n_ / d_ == n / d:\n",
|
||||||
|
" return True\n",
|
||||||
|
" except ZeroDivisionError:\n",
|
||||||
|
" return False\n",
|
||||||
|
" return False\n",
|
||||||
|
"\n",
|
||||||
|
"assert(is_curious(49, 98) == True)\n",
|
||||||
|
"assert(is_curious(30, 50) == False)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 25,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"[(16, 64), (19, 95), (26, 65), (49, 98)]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 25,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"fs = [(n, d) for n in range(10, 100) for d in range(n, 100) if is_curious(n, d)]\n",
|
||||||
|
"fs"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 32,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"name": "stdout",
|
||||||
|
"output_type": "stream",
|
||||||
|
"text": [
|
||||||
|
"387296/38729600\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"n = 1\n",
|
||||||
|
"d = 1\n",
|
||||||
|
"for n_, d_ in fs:\n",
|
||||||
|
" n *= n_\n",
|
||||||
|
" d *= d_\n",
|
||||||
|
"\n",
|
||||||
|
"print(\"{}/{}\".format(n, d))"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Now we can see that the solution is $100$. But actually it would be nice to calculate the GCD."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 40,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"100"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 40,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"def gcd_euclid(a, b):\n",
|
||||||
|
" if a == b:\n",
|
||||||
|
" return a\n",
|
||||||
|
" elif a > b:\n",
|
||||||
|
" return gcd_euclid(a - b, b)\n",
|
||||||
|
" elif a < b:\n",
|
||||||
|
" return gcd_euclid(a, b - a)\n",
|
||||||
|
" \n",
|
||||||
|
"gcd_nd = gcd_euclid(n, d)\n",
|
||||||
|
"\n",
|
||||||
|
"s = d // gcd_nd\n",
|
||||||
|
"assert(s == 100)\n",
|
||||||
|
"s"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"completion_date": "Mon, 12 Feb 2018, 17:29",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"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.5.4"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"gcd",
|
||||||
|
"curious",
|
||||||
|
"faction"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
268
ipython/EulerProblem034.html
Normal file
268
ipython/EulerProblem034.html
Normal file
File diff suppressed because one or more lines are too long
101
ipython/EulerProblem034.ipynb
Normal file
101
ipython/EulerProblem034.ipynb
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Euler Problem 34\n",
|
||||||
|
"\n",
|
||||||
|
"145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.\n",
|
||||||
|
"\n",
|
||||||
|
"Find the sum of all numbers which are equal to the sum of the factorial of their digits.\n",
|
||||||
|
"\n",
|
||||||
|
"Note: as 1! = 1 and 2! = 2 are not sums they are not included."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The algorithm for checking if a number is curious should be efficient. The more difficult thing is to select the upper bound for the brute force. It can be seen that $9 999 999 < 9! * 7$. Hence we can select $10^7$ as our bound."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from math import factorial\n",
|
||||||
|
"\n",
|
||||||
|
"def is_curious(n):\n",
|
||||||
|
" s = sum([factorial(int(d)) for d in str(n)])\n",
|
||||||
|
" return n == s\n",
|
||||||
|
"\n",
|
||||||
|
"assert(is_curious(145) == True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": true
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"s = sum([n for n in range(3, 10**7) if is_curious(n)])"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {
|
||||||
|
"collapsed": false
|
||||||
|
},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"40730"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 3,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"assert(s == 40730)\n",
|
||||||
|
"s"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"completion_date": "Mon, 12 Feb 2018, 17:57",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3",
|
||||||
|
"language": "python",
|
||||||
|
"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.5.4"
|
||||||
|
},
|
||||||
|
"tags": [
|
||||||
|
"factorial",
|
||||||
|
"brute force"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 0
|
||||||
|
}
|
||||||
@@ -492,6 +492,34 @@
|
|||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>Problem 033</td>
|
||||||
|
<td>Mon, 12 Feb 2018, 17:29</td>
|
||||||
|
<td><a href="EulerProblem033.html">Problem 033</a></td>
|
||||||
|
<td>
|
||||||
|
|
||||||
|
<kbd>gcd</kbd>
|
||||||
|
|
||||||
|
<kbd>curious</kbd>
|
||||||
|
|
||||||
|
<kbd>faction</kbd>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>Problem 034</td>
|
||||||
|
<td>Mon, 12 Feb 2018, 17:57</td>
|
||||||
|
<td><a href="EulerProblem034.html">Problem 034</a></td>
|
||||||
|
<td>
|
||||||
|
|
||||||
|
<kbd>factorial</kbd>
|
||||||
|
|
||||||
|
<kbd>brute force</kbd>
|
||||||
|
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>Problem 067</td>
|
<td>Problem 067</td>
|
||||||
<td>Fri, 5 Sep 2014, 07:36</td>
|
<td>Fri, 5 Sep 2014, 07:36</td>
|
||||||
|
|||||||
Reference in New Issue
Block a user