From ce2544de2744f336338b9d38aac950838b54049a Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 12 Feb 2018 18:59:01 +0100 Subject: [PATCH] Finished 33 and 34. --- ipython/EulerProblem033.html | 353 ++++++++++++++++++++++++++++++++++ ipython/EulerProblem033.ipynb | 172 +++++++++++++++++ ipython/EulerProblem034.html | 268 ++++++++++++++++++++++++++ ipython/EulerProblem034.ipynb | 101 ++++++++++ ipython/index.html | 28 +++ 5 files changed, 922 insertions(+) create mode 100644 ipython/EulerProblem033.html create mode 100644 ipython/EulerProblem033.ipynb create mode 100644 ipython/EulerProblem034.html create mode 100644 ipython/EulerProblem034.ipynb diff --git a/ipython/EulerProblem033.html b/ipython/EulerProblem033.html new file mode 100644 index 0000000..88af0f7 --- /dev/null +++ b/ipython/EulerProblem033.html @@ -0,0 +1,353 @@ + + + + + +EulerProblem033 + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+
+

Euler Problem 33

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.

+

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

+

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.

+

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.

+ +
+
+
+
+
+
+
+
+

We start write a function which checks if a number is curios and then brute force.

+ +
+
+
+
+
+
In [21]:
+
+
+
def is_curious(n, d):
+    assert(len(str(n)) == 2 and len(str(d)) == 2)
+    if n == d:
+        return False
+    for i in range(1, 10):
+        if str(i) in str(n) and str(i) in str(d):
+            try:
+                n_ = int(str(n).replace(str(i), ""))
+                d_ = int(str(d).replace(str(i), ""))
+            except ValueError:
+                return False
+            try:
+                if n_ / d_ == n / d:
+                    return True
+            except ZeroDivisionError:
+                return False
+    return False
+
+assert(is_curious(49, 98) == True)
+assert(is_curious(30, 50) == False)
+
+ +
+
+
+ +
+
+
+
In [25]:
+
+
+
fs = [(n, d) for n in range(10, 100) for d in range(n, 100) if is_curious(n, d)]
+fs
+
+ +
+
+
+ +
+
+ + +
Out[25]:
+ + +
+
[(16, 64), (19, 95), (26, 65), (49, 98)]
+
+ +
+ +
+
+ +
+
+
+
In [32]:
+
+
+
n = 1
+d = 1
+for n_, d_ in fs:
+    n *= n_
+    d *= d_
+
+print("{}/{}".format(n, d))
+
+ +
+
+
+ +
+
+ + +
+
+
387296/38729600
+
+
+
+ +
+
+ +
+
+
+
+
+
+

Now we can see that the solution is $100$. But actually it would be nice to calculate the GCD.

+ +
+
+
+
+
+
In [40]:
+
+
+
def gcd_euclid(a, b):
+    if a == b:
+        return a
+    elif a > b:
+        return gcd_euclid(a - b, b)
+    elif a < b:
+        return gcd_euclid(a, b - a)
+        
+gcd_nd = gcd_euclid(n, d)
+
+s = d // gcd_nd
+assert(s == 100)
+s
+
+ +
+
+
+ +
+
+ + +
Out[40]:
+ + +
+
100
+
+ +
+ +
+
+ +
+
+
+ + diff --git a/ipython/EulerProblem033.ipynb b/ipython/EulerProblem033.ipynb new file mode 100644 index 0000000..9a4119c --- /dev/null +++ b/ipython/EulerProblem033.ipynb @@ -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 +} diff --git a/ipython/EulerProblem034.html b/ipython/EulerProblem034.html new file mode 100644 index 0000000..99633f1 --- /dev/null +++ b/ipython/EulerProblem034.html @@ -0,0 +1,268 @@ + + + + + +EulerProblem034 + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+
+

Euler Problem 34

145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.

+

Find the sum of all numbers which are equal to the sum of the factorial of their digits.

+

Note: as 1! = 1 and 2! = 2 are not sums they are not included.

+ +
+
+
+
+
+
+
+
+

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.

+ +
+
+
+
+
+
In [1]:
+
+
+
from math import factorial
+
+def is_curious(n):
+    s = sum([factorial(int(d)) for d in str(n)])
+    return n == s
+
+assert(is_curious(145) == True)
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
s = sum([n for n in range(3, 10**7) if is_curious(n)])
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
assert(s == 40730)
+s
+
+ +
+
+
+ +
+
+ + +
Out[3]:
+ + +
+
40730
+
+ +
+ +
+
+ +
+
+
+ + diff --git a/ipython/EulerProblem034.ipynb b/ipython/EulerProblem034.ipynb new file mode 100644 index 0000000..59053b5 --- /dev/null +++ b/ipython/EulerProblem034.ipynb @@ -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 +} diff --git a/ipython/index.html b/ipython/index.html index 4c8bb78..1d2f52a 100644 --- a/ipython/index.html +++ b/ipython/index.html @@ -492,6 +492,34 @@ + + Problem 033 + Mon, 12 Feb 2018, 17:29 + Problem 033 + + + gcd + + curious + + faction + + + + + + Problem 034 + Mon, 12 Feb 2018, 17:57 + Problem 034 + + + factorial + + brute force + + + + Problem 067 Fri, 5 Sep 2014, 07:36