diff --git a/ipython/EulerProblem049.ipynb b/ipython/EulerProblem049.ipynb new file mode 100644 index 0000000..bc59539 --- /dev/null +++ b/ipython/EulerProblem049.ipynb @@ -0,0 +1,168 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Prime permutations (Euler Problem 49)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "collapsed": true + }, + "source": [ + "[https://projecteuler.net/problem=49](https://projecteuler.net/problem=49)\n", + "\n", + "The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.\n", + "\n", + "There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.\n", + "\n", + "What 12-digit number do you form by concatenating the three terms in this sequence?" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [ + "def sieve_of_eratosthenes(number):\n", + " primes = []\n", + " prospects = [n for n in range(2, number)]\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 > number:\n", + " break\n", + " return primes + prospects\n", + "\n", + "ps = [p for p in sieve_of_eratosthenes(10000) if p > 1000]" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [], + "source": [ + "d = {}\n", + "for p in ps:\n", + " s = \"\".join(sorted(str(p)))\n", + " try:\n", + " d[s].append(p)\n", + " except KeyError:\n", + " d[s] = [p]\n", + " \n", + "pss = [value for key, value in d.items() if len(value) >= 3]" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[[1487, 4817, 8147], [2969, 6299, 9629]]\n" + ] + } + ], + "source": [ + "def find_increasing_sequence(xs):\n", + " deltas = [(xs[j] - xs[i], i, j)\n", + " for i in range(0, len(xs) - 1)\n", + " for j in range(i + 1, len(xs))\n", + " ]\n", + " d = {}\n", + " for delta, i, j in deltas:\n", + " if delta in d and d[delta][-1] == i:\n", + " d[delta].append(j)\n", + " else:\n", + " d[delta] = [i, j]\n", + " for delta, sequence in d.items():\n", + " if len(sequence) == 3:\n", + " return [xs[index] for index in sequence]\n", + " return []\n", + "\n", + "s = [find_increasing_sequence(ps) for ps in pss if find_increasing_sequence(ps)]\n", + "print(s)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "296962999629\n" + ] + } + ], + "source": [ + "s = int(\"\".join(map(str, s[1])))\n", + "assert(s == 296962999629)\n", + "print(s)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Congratulations, the answer you gave to problem 49 is correct.\n", + "\n", + "You are the 49654th person to have solved this problem.\n", + "\n", + "Nice work, failx, you've just advanced to Level 2 . \n", + "46903 members (5.43%) have made it this far.\n", + "\n", + "This problem had a difficulty rating of 5%. The highest difficulty rating you have solved remains at 5%. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "completion_date": "Sun, 23 Dec 2018, 01:10", + "kernelspec": { + "display_name": "Python 3", + "language": "python3.6", + "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.6.5" + }, + "tags": [ + "permutations", + "primes", + "sequence" + ] + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/ipython/html/EulerProblem049.html b/ipython/html/EulerProblem049.html new file mode 100644 index 0000000..3dfe3f6 --- /dev/null +++ b/ipython/html/EulerProblem049.html @@ -0,0 +1,11918 @@ + + + + +EulerProblem049 + + + + + + + + + + + + + + +
+
+
+
+
+
+

Prime permutations (Euler Problem 49)

Back to overview.

+
+
+
+
+
+
+
+

https://projecteuler.net/problem=49

+

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

+

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

+

What 12-digit number do you form by concatenating the three terms in this sequence?

+
+
+
+
+
+
In [20]:
+
+
+
def sieve_of_eratosthenes(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:
+            break
+    return primes + prospects
+
+ps = [p for p in sieve_of_eratosthenes(10000) if p > 1000]
+
+
+
+
+
+
+
+
In [21]:
+
+
+
d = {}
+for p in ps:
+    s = "".join(sorted(str(p)))
+    try:
+        d[s].append(p)
+    except KeyError:
+        d[s] = [p]
+        
+pss = [value for key, value in d.items() if len(value) >= 3]
+
+
+
+
+
+
+
+
In [48]:
+
+
+
def find_increasing_sequence(xs):
+    deltas = [(xs[j] - xs[i], i, j)
+        for i in range(0, len(xs) - 1)
+        for j in range(i + 1, len(xs))
+    ]
+    d = {}
+    for delta, i, j in deltas:
+        if delta in d and d[delta][-1] == i:
+            d[delta].append(j)
+        else:
+            d[delta] = [i, j]
+    for delta, sequence in d.items():
+        if len(sequence) == 3:
+            return [xs[index] for index in sequence]
+    return []
+
+s = [find_increasing_sequence(ps) for ps in pss if find_increasing_sequence(ps)]
+print(s)
+
+
+
+
+
+
+
+
+
+
[[1487, 4817, 8147], [2969, 6299, 9629]]
+
+
+
+
+
+
+
+
+
In [49]:
+
+
+
s = int("".join(map(str, s[1])))
+assert(s == 296962999629)
+print(s)
+
+
+
+
+
+
+
+
+
+
296962999629
+
+
+
+
+
+
+
+
+
+
+

Congratulations, the answer you gave to problem 49 is correct.

+

You are the 49654th person to have solved this problem.

+

Nice work, failx, you've just advanced to Level 2 . +46903 members (5.43%) have made it this far.

+

This problem had a difficulty rating of 5%. The highest difficulty rating you have solved remains at 5%.

+
+
+
+
+
+
In [ ]:
+
+
+
 
+
+
+
+
+
+
+
+ + diff --git a/ipython/html/index.html b/ipython/html/index.html index 44a09c8..56f5624 100644 --- a/ipython/html/index.html +++ b/ipython/html/index.html @@ -775,6 +775,22 @@ + + + Problem 049 + Sun, 23 Dec 2018, 01:10 + + + permutations + + primes + + sequence + + + + + Problem 067