From 147a13a01f9f339595f2d674899a8c370ef15565 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Mon, 24 Dec 2018 17:15:19 -0500 Subject: [PATCH] Tried to solve 55 till 57. --- ipython/EulerProblem055.ipynb | 79 +++++++++++++++++++++++++++ ipython/EulerProblem056.ipynb | 41 +++++++++++++- ipython/EulerProblem057.ipynb | 81 ++++++++++++++++++++++++++++ ipython/html/EulerProblem055.html | 88 +++++++++++++++++++++++++++++++ ipython/html/EulerProblem056.html | 46 +++++++++++++++- ipython/html/EulerProblem057.html | 86 ++++++++++++++++++++++++++++++ 6 files changed, 417 insertions(+), 4 deletions(-) diff --git a/ipython/EulerProblem055.ipynb b/ipython/EulerProblem055.ipynb index 181876b..c962357 100644 --- a/ipython/EulerProblem055.ipynb +++ b/ipython/EulerProblem055.ipynb @@ -36,6 +36,85 @@ "NOTE: Wording was modified slightly on 24 April 2007 to emphasise the theoretical nature of Lychrel numbers." ] }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "def get_digits(n):\n", + " d = []\n", + " while n:\n", + " d.append(n % 10)\n", + " n //= 10\n", + " return d\n", + "\n", + "def is_pilandrome(n):\n", + " ds = get_digits(n)\n", + " len_ds = len(ds)\n", + " if len_ds < 2:\n", + " return True\n", + " for i in range(0, len_ds // 2):\n", + " if ds[i] != ds[len_ds - i - 1]:\n", + " return False\n", + " return True\n", + "\n", + "assert(is_pilandrome(1337) == False)\n", + "assert(is_pilandrome(1331))\n", + "assert(is_pilandrome(131))\n", + "assert(is_pilandrome(132) == False)\n", + "\n", + "\n", + "def get_digit_inverse(n):\n", + " ds = get_digits(n)\n", + " base = 1\n", + " i = 0\n", + " for d in ds[::-1]:\n", + " i += (base * d)\n", + " base *= 10\n", + " return i\n", + "\n", + "assert(get_digit_inverse(47) == 74)\n", + "assert(get_digit_inverse(47) == 74)" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [], + "source": [ + "def is_not_lychrel(n, iterations=50):\n", + " for i in range(0, iterations):\n", + " n = n + get_digit_inverse(n)\n", + " if is_pilandrome(n):\n", + " return (i + 1)\n", + " return 0\n", + "\n", + "assert(is_not_lychrel(47) == 1)\n", + "assert(is_not_lychrel(349) == 3)\n", + "assert(is_not_lychrel(10677, 100) == 53)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "249\n" + ] + } + ], + "source": [ + "lychrels = [n for n in range(1, 10000) if is_not_lychrel(n) == 0]\n", + "s = len(lychrels)\n", + "print(s)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/ipython/EulerProblem056.ipynb b/ipython/EulerProblem056.ipynb index 80d0f5f..d372239 100644 --- a/ipython/EulerProblem056.ipynb +++ b/ipython/EulerProblem056.ipynb @@ -13,9 +13,46 @@ "collapsed": true }, "source": [ - "A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.\n", + "A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.\n", "\n", - "Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?" + "Considering natural numbers of the form, $a^b$, where $a, b < 100$, what is the maximum digital sum?" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def get_digit_sum(n):\n", + " s = 0\n", + " while n != 0:\n", + " s += (n % 10)\n", + " n //= 10\n", + " return s\n", + "\n", + "assert(get_digit_sum(1337) == 14)\n", + "assert(get_digit_sum(100**100) == 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "972\n" + ] + } + ], + "source": [ + "s = max([get_digit_sum(a**b) for a in range(1, 100) for b in range(1, 100)])\n", + "print(s)" ] }, { diff --git a/ipython/EulerProblem057.ipynb b/ipython/EulerProblem057.ipynb index a3e8689..5415e27 100644 --- a/ipython/EulerProblem057.ipynb +++ b/ipython/EulerProblem057.ipynb @@ -34,6 +34,87 @@ "In the first one-thousand expansions, how many fractions contain a numerator with more digits than denominator?" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def get_digit_count(n):\n", + " c = 1\n", + " while n:\n", + " n += 1\n", + " n //= 10\n", + " return d" + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def gcd(a, b):\n", + " if b > a:\n", + " a, b = b, a\n", + " while a % b != 0:\n", + " a, b = b, a % b\n", + " return b\n", + " \n", + "assert(gcd(100, 35) == 5)\n", + "\n", + "def add_fractions(n1, d1, n2, d2):\n", + " d = d1 * d2\n", + " n1 = n1 * (d // d1)\n", + " n2 = n2 * (d // d2)\n", + " n = n1 + n2\n", + " p = gcd(n, d)\n", + " return (n // p, d // p)" + ] + }, + { + "cell_type": "code", + "execution_count": 54, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def next_expension(n, d):\n", + " n, d = add_fractions(1, 1, n, d)\n", + " return add_fractions(1, 1, d, n)\n", + "\n", + "c = 0\n", + "\n", + "n, d = (3, 2)\n", + "for i in range(1000):\n", + " if get_digit_count(n)> get_digit_count(d):\n", + " c += 1\n", + " n, d = next_expension(n, d)" + ] + }, + { + "cell_type": "code", + "execution_count": 55, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "153\n" + ] + } + ], + "source": [ + "s = c\n", + "print(s)" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/ipython/html/EulerProblem055.html b/ipython/html/EulerProblem055.html index 7661dcf..5b5fd93 100644 --- a/ipython/html/EulerProblem055.html +++ b/ipython/html/EulerProblem055.html @@ -11794,6 +11794,94 @@ div#notebook {
+
In [10]:
+
+
+
def get_digits(n):
+    d = []
+    while n:
+        d.append(n % 10)
+        n //= 10
+    return d
+
+def is_pilandrome(n):
+    ds = get_digits(n)
+    len_ds = len(ds)
+    if len_ds < 2:
+        return True
+    for i in range(0, len_ds // 2):
+        if ds[i] != ds[len_ds - i - 1]:
+            return False
+    return True
+
+assert(is_pilandrome(1337) == False)
+assert(is_pilandrome(1331))
+assert(is_pilandrome(131))
+assert(is_pilandrome(132) == False)
+
+
+def get_digit_inverse(n):
+    ds = get_digits(n)
+    base = 1
+    i = 0
+    for d in ds[::-1]:
+        i += (base * d)
+        base *= 10
+    return i
+
+assert(get_digit_inverse(47) == 74)
+assert(get_digit_inverse(47) == 74)
+
+
+
+
+
+
+
+
In [24]:
+
+
+
def is_not_lychrel(n, iterations=50):
+    for i in range(0, iterations):
+        n = n + get_digit_inverse(n)
+        if is_pilandrome(n):
+            return (i + 1)
+    return 0
+
+assert(is_not_lychrel(47) == 1)
+assert(is_not_lychrel(349) == 3)
+assert(is_not_lychrel(10677, 100) == 53)
+
+
+
+
+
+
+
+
In [27]:
+
+
+
lychrels = [n for n in range(1, 10000) if is_not_lychrel(n) == 0]
+s = len(lychrels)
+print(s)
+
+
+
+
+
+
+
+
+
+
249
+
+
+
+
+
+
+
+
In [ ]:
diff --git a/ipython/html/EulerProblem056.html b/ipython/html/EulerProblem056.html index 3d3f400..0fa3cdb 100644 --- a/ipython/html/EulerProblem056.html +++ b/ipython/html/EulerProblem056.html @@ -11778,8 +11778,50 @@ div#notebook {
-

A googol (10100) is a massive number: one followed by one-hundred zeros; 100100 is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

-

Considering natural numbers of the form, ab, where a, b < 100, what is the maximum digital sum?

+

A googol ($10^{100}$) is a massive number: one followed by one-hundred zeros; $100^{100}$ is almost unimaginably large: one followed by two-hundred zeros. Despite their size, the sum of the digits in each number is only 1.

+

Considering natural numbers of the form, $a^b$, where $a, b < 100$, what is the maximum digital sum?

+
+
+
+
+
+
In [3]:
+
+
+
def get_digit_sum(n):
+    s = 0
+    while n != 0:
+        s += (n % 10)
+        n //= 10
+    return s
+
+assert(get_digit_sum(1337) == 14)
+assert(get_digit_sum(100**100) == 1)
+
+
+
+
+
+
+
+
In [8]:
+
+
+
s = max([get_digit_sum(a**b) for a in range(1, 100) for b in range(1, 100)])
+print(s)
+
+
+
+
+
+
+
+
+
+
972
+
+
+
diff --git a/ipython/html/EulerProblem057.html b/ipython/html/EulerProblem057.html index 362829d..6eaef0a 100644 --- a/ipython/html/EulerProblem057.html +++ b/ipython/html/EulerProblem057.html @@ -11796,6 +11796,92 @@ div#notebook {
In [ ]:
+
def get_digit_count(n):
+    c = 1
+    while n:
+        n += 1
+        n //= 10
+    return d
+
+
+
+
+
+
+
+
In [44]:
+
+
+
def gcd(a, b):
+    if b > a:
+        a, b = b, a
+    while a % b != 0:
+        a, b = b, a % b
+    return b
+        
+assert(gcd(100, 35) == 5)
+
+def add_fractions(n1, d1, n2, d2):
+    d = d1 * d2
+    n1 = n1 * (d // d1)
+    n2 = n2 * (d // d2)
+    n = n1 + n2
+    p = gcd(n, d)
+    return (n // p, d // p)
+
+
+
+
+
+
+
+
In [54]:
+
+
+
def next_expension(n, d):
+    n, d = add_fractions(1, 1, n, d)
+    return add_fractions(1, 1, d, n)
+
+c = 0
+
+n, d = (3, 2)
+for i in range(1000):
+    if get_digit_count(n)> get_digit_count(d):
+        c += 1
+    n, d = next_expension(n, d)
+
+
+
+
+
+
+
+
In [55]:
+
+
+
s = c
+print(s)
+
+
+
+
+
+
+
+
+
+
153
+
+
+
+
+
+
+
+
+
In [ ]:
+
+