From 01daa27f86c1a300f858f458c2b264c5d8317ad6 Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 13 Feb 2018 10:17:37 +0100 Subject: [PATCH] Finished 35. Had epiphany when lying in bed. Misinterpeted circular to mean permutations. --- ipython/EulerProblem035.html | 373 ++++++++++++++++++++++++++++++++++ ipython/EulerProblem035.ipynb | 88 +++++--- ipython/index.html | 13 ++ 3 files changed, 441 insertions(+), 33 deletions(-) create mode 100644 ipython/EulerProblem035.html diff --git a/ipython/EulerProblem035.html b/ipython/EulerProblem035.html new file mode 100644 index 0000000..06910be --- /dev/null +++ b/ipython/EulerProblem035.html @@ -0,0 +1,373 @@ + + + + + +EulerProblem035 + + + + + + + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+
+

Euler Problem 35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

+

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

+

How many circular primes are there below one million?

+ +
+
+
+
+
+
+
+
+

First we get all primes into a look-up table. Then we iterate them and check whether they are circular.

+ +
+
+
+
+
+
In [1]:
+
+
+
def get_primes_smaller(number):
+    primes = []
+    prospects = [n for n in range(2, number)]
+    while prospects:
+        p = prospects[0]
+        prospects = [x for x in prospects if x % p != 0]
+        primes.append(p)
+        if p * p > number:
+            return primes + prospects
+    return primes
+
+ps = get_primes_smaller(1000000)
+
+ +
+
+
+ +
+
+
+
In [2]:
+
+
+
def get_combinations(xs):
+    if not xs:
+        return []
+    rs = []
+    for i in range(len(xs)):
+        yss = get_combinations(xs[:i] + xs[i + 1:])
+        if not yss:
+            rs.append(xs[i])
+        for ys in yss:
+            rs.append(xs[i] + ys)
+    return rs
+
+assert(get_combinations("ab") == ["ab", "ba"])
+
+ +
+
+
+ +
+
+
+
In [3]:
+
+
+
from itertools import permutations
+prime_set = set(ps)
+
+def is_circular(p):
+    cs = permutations(str(p))
+    for c in cs:
+        if not int("".join(c)) in prime_set:
+            return False
+    return True
+
+assert(is_circular("2") == True)
+assert(is_circular("11") == True)
+assert(is_circular("47") == False)
+
+ +
+
+
+ +
+
+
+
In [4]:
+
+
+
s = len([p for p in ps if is_circular(p)])
+print("False solution {}".format(s))
+
+ +
+
+
+ +
+
+ + +
+
+
False solution 22
+
+
+
+ +
+
+ +
+
+
+
+
+
+

We did not read the problem properly. Cycles are obviously not the same as permutations. If we change that we should get the solution in no time.

+ +
+
+
+
+
+
In [5]:
+
+
+
def cyles(xs):
+    if len(xs) <= 1:
+        return xs
+    return [xs[i:] + xs[:i]  for i in range(len(xs))]
+
+prime_set = set(ps)
+
+def is_circular(p):
+    cs = cyles(str(p))
+    for c in cs:
+        if not int("".join(c)) in prime_set:
+            return False
+    return True
+
+ +
+
+
+ +
+
+
+
In [6]:
+
+
+
s = len([p for p in ps if is_circular(p)])
+s
+
+ +
+
+
+ +
+
+ + +
Out[6]:
+ + +
+
55
+
+ +
+ +
+
+ +
+
+
+ + diff --git a/ipython/EulerProblem035.ipynb b/ipython/EulerProblem035.ipynb index 2759c22..1983ad3 100644 --- a/ipython/EulerProblem035.ipynb +++ b/ipython/EulerProblem035.ipynb @@ -22,19 +22,11 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 1, "metadata": { "collapsed": false }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "78498\n" - ] - } - ], + "outputs": [], "source": [ "def get_primes_smaller(number):\n", " primes = []\n", @@ -52,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 2, "metadata": { "collapsed": false }, @@ -75,7 +67,7 @@ }, { "cell_type": "code", - "execution_count": 25, + "execution_count": 3, "metadata": { "collapsed": false }, @@ -98,18 +90,7 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [ - "s = [p for p in ps if is_circular(p)]" - ] - }, - { - "cell_type": "code", - "execution_count": 28, + "execution_count": 4, "metadata": { "collapsed": false }, @@ -118,34 +99,71 @@ "name": "stdout", "output_type": "stream", "text": [ - "[2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, 97, 113, 131, 199, 311, 337, 373, 733, 919, 991]\n" + "False solution 22\n" ] } ], "source": [ - "print(s)" + "s = len([p for p in ps if is_circular(p)])\n", + "print(\"False solution {}\".format(s))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We did not read the problem properly. Cycles are obviously not the same as permutations. If we change that we should get the solution in no time." ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [], - "source": [] + "source": [ + "def cyles(xs):\n", + " if len(xs) <= 1:\n", + " return xs\n", + " return [xs[i:] + xs[:i] for i in range(len(xs))]\n", + "\n", + "prime_set = set(ps)\n", + "\n", + "def is_circular(p):\n", + " cs = cyles(str(p))\n", + " for c in cs:\n", + " if not int(\"\".join(c)) in prime_set:\n", + " return False\n", + " return True" + ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "metadata": { - "collapsed": true + "collapsed": false }, - "outputs": [], - "source": [] + "outputs": [ + { + "data": { + "text/plain": [ + "55" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "s = len([p for p in ps if is_circular(p)])\n", + "s" + ] } ], "metadata": { + "completion_date": "Tue, 13 Feb 2018, 09:14", "kernelspec": { "display_name": "Python 3", "language": "python", @@ -162,7 +180,11 @@ "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.4" - } + }, + "tags": [ + "circular", + "combinations" + ] }, "nbformat": 4, "nbformat_minor": 0 diff --git a/ipython/index.html b/ipython/index.html index 1d2f52a..25c623f 100644 --- a/ipython/index.html +++ b/ipython/index.html @@ -520,6 +520,19 @@ + + Problem 035 + Tue, 13 Feb 2018, 09:14 + Problem 035 + + + circular + + combinations + + + + Problem 067 Fri, 5 Sep 2014, 07:36