169 lines
4.2 KiB
Plaintext
169 lines
4.2 KiB
Plaintext
|
{
|
||
|
"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
|
||
|
}
|