diff --git a/ipython/EulerProblem060.ipynb b/ipython/EulerProblem060.ipynb index e07f952..fb88180 100644 --- a/ipython/EulerProblem060.ipynb +++ b/ipython/EulerProblem060.ipynb @@ -20,6 +20,132 @@ "Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.\n" ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[3, 7], [7, 3], [3, 109], [109, 3], [3, 673], [673, 3], [7, 109], [109, 7], [7, 673], [673, 7], [109, 673], [673, 109]]\n" + ] + } + ], + "source": [ + "from itertools import combinations\n", + "\n", + "def concatenate_all(numbers):\n", + " result = []\n", + " for a, b in combinations(numbers, 2):\n", + " result.append([a, b])\n", + " result.append([b, a])\n", + " return result\n", + "\n", + "print(concatenate_all([3, 7, 109, 673]))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def sieve_of_eratosthenes(limit):\n", + " primes = []\n", + " prospects = [n for n in range(2, limit)]\n", + " while prospects:\n", + " p = prospects[0]\n", + " prospects = [x for x in prospects if x % p != 0]\n", + " primes.append(p)\n", + " if p * p > limit:\n", + " break\n", + " primes += prospects\n", + " return primes\n", + "\n", + "primes = sieve_of_eratosthenes(100000)\n", + "primes_set = set(primes)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [], + "source": [ + "def is_prime(n):\n", + " if n % 2 == 0:\n", + " return False\n", + " while n % 2 == 0:\n", + " n //= 2\n", + " f = 3\n", + " while f * f <= n:\n", + " if n % f == 0:\n", + " return False\n", + " else:\n", + " f += 2 \n", + " return True\n", + "\n", + "def concatentation_is_prime(a, b):\n", + " ab = int(str(a) + str(b))\n", + " ba = int(str(b) + str(a))\n", + " if is_prime(ab) and is_prime(ba):\n", + " return True\n", + " else:\n", + " return False\n", + " \n", + "assert(concatentation_is_prime(673, 3))\n", + "assert(concatentation_is_prime(673, 1) == False)" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[13, 5197, 5701, 6733, 8389]\n", + "26033\n" + ] + } + ], + "source": [ + "potentials = []\n", + "new_potentials = []\n", + "done = False\n", + "for p in primes:\n", + " if done:\n", + " break\n", + " potentials += new_potentials\n", + " new_potentials = []\n", + " for potential in potentials:\n", + " all_concatenations_prime = True\n", + " for prime in potential:\n", + " if not concatentation_is_prime(p, prime):\n", + " all_concatenations_prime = False\n", + " break\n", + " if all_concatenations_prime:\n", + " new_potential = list(potential)\n", + " new_potential.append(p)\n", + " if len(new_potential) > 4:\n", + " print(new_potential)\n", + " s = sum(new_potential)\n", + " done = True\n", + " break\n", + " new_potentials.append(new_potential)\n", + " if p < 15:\n", + " potentials.append([p])\n", + "\n", + "print(s)\n", + "assert(s == 26033)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -31,7 +157,7 @@ } ], "metadata": { - "completion_date": "", + "completion_date": "Sun, 6 Jan 2019, 05:00", "kernelspec": { "display_name": "Python 3", "language": "python3.6", @@ -49,7 +175,10 @@ "pygments_lexer": "ipython3", "version": "3.6.5" }, - "tags": [] + "tags": [ + "prime", + "pairs" + ] }, "nbformat": 4, "nbformat_minor": 2 diff --git a/ipython/EulerProblem061.ipynb b/ipython/EulerProblem061.ipynb index 1b83761..248912a 100644 --- a/ipython/EulerProblem061.ipynb +++ b/ipython/EulerProblem061.ipynb @@ -37,6 +37,93 @@ "Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "def get_four_digit_numbers(function):\n", + " r = []\n", + " n = 1\n", + " f = function\n", + " while f(n) < 10000:\n", + " if f(n) > 999:\n", + " r.append(f(n))\n", + " n += 1\n", + " return r\n", + "\n", + "triangles = get_four_digit_numbers(lambda n: n * (n + 1) // 2)\n", + "squares = get_four_digit_numbers(lambda n: n**2)\n", + "pentas = get_four_digit_numbers(lambda n: n * (3 * n - 1) // 2)\n", + "hexas = get_four_digit_numbers(lambda n: n * (2 * n - 1))\n", + "heptas = get_four_digit_numbers(lambda n: n * (5*n - 3) // 2)\n", + "octas = get_four_digit_numbers(lambda n: n * (3*n - 2))" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def is_cyclic(a, b):\n", + " return str(a)[-2:] == str(b)[:2]\n", + "\n", + "assert(is_cyclic(3328, 2877))\n", + "assert(is_cyclic(3329, 2877) == False)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[8256, 5625, 2512, 1281, 8128, 2882]\n", + "28684\n" + ] + } + ], + "source": [ + "def search_solution(aggregator, polygonals):\n", + " if not polygonals:\n", + " if is_cyclic(aggregator[-1], aggregator[0]):\n", + " return aggregator\n", + " else:\n", + " return []\n", + "\n", + " if not aggregator:\n", + " for polygonal in polygonals:\n", + " for number in polygonal:\n", + " aggregator.append(number)\n", + " s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n", + " if s:\n", + " return s\n", + " aggregator.pop()\n", + "\n", + " for polygonal in polygonals:\n", + " for number in polygonal:\n", + " if is_cyclic(aggregator[-1], number) and not number in aggregator:\n", + " aggregator.append(number)\n", + " s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n", + " if s:\n", + " return s\n", + " aggregator.pop()\n", + " return []\n", + "\n", + "s = search_solution([], [triangles, squares, pentas, hexas, heptas, octas])\n", + "print(s)\n", + "s = sum(s)\n", + "print(s)\n", + "assert(s == 28684)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -48,7 +135,7 @@ } ], "metadata": { - "completion_date": "", + "completion_date": "Sun, 6 Jan 2019, 05:32", "kernelspec": { "display_name": "Python 3", "language": "python3.6", @@ -66,7 +153,10 @@ "pygments_lexer": "ipython3", "version": "3.6.5" }, - "tags": [] + "tags": [ + "search", + "cyclic" + ] }, "nbformat": 4, "nbformat_minor": 2 diff --git a/ipython/EulerProblem062.ipynb b/ipython/EulerProblem062.ipynb index 16f56cd..48180c3 100644 --- a/ipython/EulerProblem062.ipynb +++ b/ipython/EulerProblem062.ipynb @@ -20,6 +20,51 @@ "Find the smallest cube for which exactly five permutations of its digits are cube." ] }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "cubes = [n * n * n for n in range(1, 5000)]\n", + "cubes_set = set(cubes)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[127035954683, 352045367981, 373559126408, 569310543872, 589323567104]\n", + "127035954683\n" + ] + } + ], + "source": [ + "solutions = {}\n", + "\n", + "for n in range(1, 10000):\n", + " cube = n * n * n\n", + " try:\n", + " key = \"\".join(sorted(str(cube)))\n", + " solutions[key].append(cube)\n", + " if len(solutions[key]) > 4:\n", + " print(solutions[key])\n", + " s = solutions[key][0]\n", + " break\n", + " except KeyError:\n", + " solutions[key] = [cube]\n", + "\n", + "print(s)\n", + "assert(s == 127035954683)" + ] + }, { "cell_type": "code", "execution_count": null, @@ -31,7 +76,7 @@ } ], "metadata": { - "completion_date": "", + "completion_date": "Sun, 6 Jan 2019, 05:47", "kernelspec": { "display_name": "Python 3", "language": "python3.6", @@ -49,7 +94,9 @@ "pygments_lexer": "ipython3", "version": "3.6.5" }, - "tags": [] + "tags": [ + "cubes" + ] }, "nbformat": 4, "nbformat_minor": 2 diff --git a/ipython/html/EulerProblem060.html b/ipython/html/EulerProblem060.html index d3c2bac..566915a 100644 --- a/ipython/html/EulerProblem060.html +++ b/ipython/html/EulerProblem060.html @@ -11786,6 +11786,144 @@ div#notebook {
+
In [1]:
+
+
+
from itertools import combinations
+
+def concatenate_all(numbers):
+    result = []
+    for a, b in combinations(numbers, 2):
+        result.append([a, b])
+        result.append([b, a])
+    return result
+
+print(concatenate_all([3, 7, 109, 673]))
+
+
+
+
+
+
+
+
+
+
[[3, 7], [7, 3], [3, 109], [109, 3], [3, 673], [673, 3], [7, 109], [109, 7], [7, 673], [673, 7], [109, 673], [673, 109]]
+
+
+
+
+
+
+
+
+
In [2]:
+
+
+
def sieve_of_eratosthenes(limit):
+    primes = []
+    prospects = [n for n in range(2, limit)]
+    while prospects:
+        p = prospects[0]
+        prospects = [x for x in prospects if x % p != 0]
+        primes.append(p)
+        if p * p > limit:
+            break
+    primes += prospects
+    return primes
+
+primes = sieve_of_eratosthenes(100000)
+primes_set = set(primes)
+
+
+
+
+
+
+
+
In [3]:
+
+
+
def is_prime(n):
+    if n % 2 == 0:
+        return False
+    while n % 2 == 0:
+        n //= 2
+    f = 3
+    while f * f <= n:
+        if n % f == 0:
+            return False
+        else:
+            f += 2   
+    return True
+
+def concatentation_is_prime(a, b):
+    ab = int(str(a) + str(b))
+    ba = int(str(b) + str(a))
+    if is_prime(ab) and is_prime(ba):
+        return True
+    else:
+        return False
+    
+assert(concatentation_is_prime(673, 3))
+assert(concatentation_is_prime(673, 1) == False)
+
+
+
+
+
+
+
+
In [4]:
+
+
+
potentials = []
+new_potentials = []
+done = False
+for p in primes:
+    if done:
+        break
+    potentials += new_potentials
+    new_potentials = []
+    for potential in potentials:
+        all_concatenations_prime = True
+        for prime in potential:
+            if not concatentation_is_prime(p, prime):
+                all_concatenations_prime = False
+                break
+        if all_concatenations_prime:
+            new_potential = list(potential)
+            new_potential.append(p)
+            if len(new_potential) > 4:
+                print(new_potential)
+                s = sum(new_potential)
+                done = True
+                break
+            new_potentials.append(new_potential)
+    if p < 15:
+        potentials.append([p])
+
+print(s)
+assert(s == 26033)
+
+
+
+
+
+
+
+
+
+
[13, 5197, 5701, 6733, 8389]
+26033
+
+
+
+
+
+
+
+
In [ ]:
diff --git a/ipython/html/EulerProblem061.html b/ipython/html/EulerProblem061.html index 60d6cb9..c164f7c 100644 --- a/ipython/html/EulerProblem061.html +++ b/ipython/html/EulerProblem061.html @@ -11796,6 +11796,100 @@ Find the sum of the only ordered set of six cyclic 4-digit numbers for which eac
+
In [1]:
+
+
+
def get_four_digit_numbers(function):
+    r = []
+    n = 1
+    f = function
+    while f(n) < 10000:
+        if f(n) > 999:
+            r.append(f(n))
+        n += 1
+    return r
+
+triangles = get_four_digit_numbers(lambda n: n * (n + 1) // 2)
+squares = get_four_digit_numbers(lambda n: n**2)
+pentas = get_four_digit_numbers(lambda n: n * (3 * n - 1) // 2)
+hexas = get_four_digit_numbers(lambda n: n * (2 * n - 1))
+heptas = get_four_digit_numbers(lambda n: n * (5*n - 3) // 2)
+octas = get_four_digit_numbers(lambda n: n * (3*n - 2))
+
+
+
+
+
+
+
+
In [2]:
+
+
+
def is_cyclic(a, b):
+    return str(a)[-2:] == str(b)[:2]
+
+assert(is_cyclic(3328, 2877))
+assert(is_cyclic(3329, 2877) == False)
+
+
+
+
+
+
+
+
In [3]:
+
+
+
def search_solution(aggregator, polygonals):
+    if not polygonals:
+        if is_cyclic(aggregator[-1], aggregator[0]):
+            return aggregator
+        else:
+            return []
+
+    if not aggregator:
+        for polygonal in polygonals:
+            for number in polygonal:
+                aggregator.append(number)
+                s = search_solution(aggregator, [p for p in polygonals if p != polygonal])
+                if s:
+                    return s
+                aggregator.pop()
+
+    for polygonal in polygonals:
+        for number in polygonal:
+            if is_cyclic(aggregator[-1], number) and not number in aggregator:
+                aggregator.append(number)
+                s = search_solution(aggregator, [p for p in polygonals if p != polygonal])
+                if s:
+                    return s
+                aggregator.pop()
+    return []
+
+s = search_solution([], [triangles, squares, pentas, hexas, heptas, octas])
+print(s)
+s = sum(s)
+print(s)
+assert(s == 28684)
+
+
+
+
+
+
+
+
+
+
[8256, 5625, 2512, 1281, 8128, 2882]
+28684
+
+
+
+
+
+
+
+
In [ ]:
diff --git a/ipython/html/EulerProblem062.html b/ipython/html/EulerProblem062.html index c0e8cd0..b6998d2 100644 --- a/ipython/html/EulerProblem062.html +++ b/ipython/html/EulerProblem062.html @@ -11786,6 +11786,56 @@ div#notebook {
+
In [1]:
+
+
+
cubes = [n * n * n for n in range(1, 5000)]
+cubes_set = set(cubes)
+
+
+
+
+
+
+
+
In [2]:
+
+
+
solutions = {}
+
+for n in range(1, 10000):
+    cube = n * n * n
+    try:
+        key = "".join(sorted(str(cube)))
+        solutions[key].append(cube)
+        if len(solutions[key]) > 4:
+            print(solutions[key])
+            s = solutions[key][0]
+            break
+    except KeyError:
+        solutions[key] = [cube]
+
+print(s)
+assert(s == 127035954683)
+
+
+
+
+
+
+
+
+
+
[127035954683, 352045367981, 373559126408, 569310543872, 589323567104]
+127035954683
+
+
+
+
+
+
+
+
In [ ]:
diff --git a/ipython/html/index.html b/ipython/html/index.html index 23e0b80..8d72278 100644 --- a/ipython/html/index.html +++ b/ipython/html/index.html @@ -953,32 +953,42 @@ - + Problem 060 - + Sun, 6 Jan 2019, 05:00 + prime + + pairs + - + Problem 061 - + Sun, 6 Jan 2019, 05:32 + search + + cyclic + - + Problem 062 - + Sun, 6 Jan 2019, 05:47 + cubes +