euler/ipython/EulerProblem051.ipynb

190 lines
5.0 KiB
Plaintext
Raw Normal View History

2018-12-23 03:45:48 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prime digit replacements (Euler Problem 51)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=51](https://projecteuler.net/problem=51)\n",
"\n",
"By replacing the 1st digit of the 2-digit number x3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime.\n",
"\n",
"By replacing the 3rd and 4th digits of 56xx3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yielding the family: 56003, 56113, 56333, 56443, 56663, 56773, and 56993. Consequently 56003, being the first member of this family, is the smallest prime with this property.\n",
"\n",
"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."
]
},
2018-12-24 03:54:15 +01:00
{
"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)"
]
},
2018-12-23 03:45:48 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
2018-12-24 03:54:15 +01:00
"completion_date": "Sun, 23 Dec 2018, 23:47",
2018-12-23 03:45:48 +01:00
"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"
},
2018-12-24 03:54:15 +01:00
"tags": [
"prime",
"combinations",
"replacement"
]
2018-12-23 03:45:48 +01:00
},
"nbformat": 4,
"nbformat_minor": 2
}