Solved 51 and 52.
This commit is contained in:
@@ -22,6 +22,133 @@
|
||||
"Find the smallest prime which, by replacing part of the number (not necessarily adjacent digits) with the same digit, is part of an eight prime value family."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"source": [
|
||||
"We start by writing a function that takes a number and returns all possible replacements.\n",
|
||||
"\n",
|
||||
"For example 13 will return [\"x3\", \"1x\"].\n",
|
||||
"\n",
|
||||
"I actually had a version that simply replace even digits at first, but that does not work for 56333 for example, because you want to get 56xx3 and not 56xxx. So basically, we have to search for all combinations for each digit. I should probably document what each list comprehension does or I will never understand it again. Haha."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"['x3', '1x']\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"from itertools import combinations\n",
|
||||
"\n",
|
||||
"def get_replacements(n):\n",
|
||||
" n = str(n)\n",
|
||||
" xss= [[i for i in range(0, len(n)) if n[i] == d]\n",
|
||||
" for d in \"0123456789\"]\n",
|
||||
" xss = [x \n",
|
||||
" for xs in xss if xs\n",
|
||||
" for i in range(1, len(xs) + 1)\n",
|
||||
" for x in combinations(xs, i)]\n",
|
||||
" xss = [\"\".join(['x' if i in xs else d for i, d in enumerate(n)])\n",
|
||||
" for xs in xss]\n",
|
||||
" return xss\n",
|
||||
"\n",
|
||||
"#assert(get_replacements(13) == [\"x3\", \"1x\"])\n",
|
||||
"print(get_replacements(13))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"We will use our good old prime generator."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_prime(n, smaller_primes):\n",
|
||||
" for s in smaller_primes:\n",
|
||||
" if n % s == 0:\n",
|
||||
" return False\n",
|
||||
" if s * s > n:\n",
|
||||
" return True\n",
|
||||
" return True\n",
|
||||
"\n",
|
||||
"def prime_generator_function():\n",
|
||||
" primes = [2, 3, 5, 7]\n",
|
||||
" for p in primes:\n",
|
||||
" yield p\n",
|
||||
" while True:\n",
|
||||
" p += 2\n",
|
||||
" if is_prime(p, primes):\n",
|
||||
" primes.append(p)\n",
|
||||
" yield p"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[121313, 222323, 323333, 424343, 525353, 626363, 828383, 929393]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"replacements = {}\n",
|
||||
"ps = prime_generator_function()\n",
|
||||
"for p in ps:\n",
|
||||
" for r in get_replacements(p):\n",
|
||||
" try:\n",
|
||||
" replacements[r].append(p)\n",
|
||||
" if len(replacements[r]) == 8:\n",
|
||||
" print(replacements[r])\n",
|
||||
" s = replacements[r][0]\n",
|
||||
" p = 100000000000 # We have found a solution so we go home\n",
|
||||
" except KeyError:\n",
|
||||
" replacements[r] = [p]\n",
|
||||
" if p > 1000000:\n",
|
||||
" break"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"121313\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(s)\n",
|
||||
"assert(s == 121313)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -33,7 +160,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "",
|
||||
"completion_date": "Sun, 23 Dec 2018, 23:47",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python3.6",
|
||||
@@ -51,7 +178,11 @@
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.5"
|
||||
},
|
||||
"tags": []
|
||||
"tags": [
|
||||
"prime",
|
||||
"combinations",
|
||||
"replacement"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
|
||||
Reference in New Issue
Block a user