euler/ipython/EulerProblem060.ipynb

186 lines
4.7 KiB
Plaintext
Raw Normal View History

2018-12-24 16:57:03 +01:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prime pair sets (Euler Problem 60)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=60](https://projecteuler.net/problem=60)\n",
"\n",
"The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes and concatenating them in any order the result will always be prime. For example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these four primes, 792, represents the lowest sum for a set of four primes with this property.\n",
"\n",
"Find the lowest sum for a set of five primes for which any two primes concatenate to produce another prime.\n"
]
},
2019-01-06 06:50:37 +01:00
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[3, 7], [7, 3], [3, 109], [109, 3], [3, 673], [673, 3], [7, 109], [109, 7], [7, 673], [673, 7], [109, 673], [673, 109]]\n"
]
}
],
"source": [
"from itertools import combinations\n",
"\n",
"def concatenate_all(numbers):\n",
" result = []\n",
" for a, b in combinations(numbers, 2):\n",
" result.append([a, b])\n",
" result.append([b, a])\n",
" return result\n",
"\n",
"print(concatenate_all([3, 7, 109, 673]))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def sieve_of_eratosthenes(limit):\n",
" primes = []\n",
" prospects = [n for n in range(2, limit)]\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 > limit:\n",
" break\n",
" primes += prospects\n",
" return primes\n",
"\n",
"primes = sieve_of_eratosthenes(100000)\n",
"primes_set = set(primes)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def is_prime(n):\n",
" if n % 2 == 0:\n",
" return False\n",
" while n % 2 == 0:\n",
" n //= 2\n",
" f = 3\n",
" while f * f <= n:\n",
" if n % f == 0:\n",
" return False\n",
" else:\n",
" f += 2 \n",
" return True\n",
"\n",
"def concatentation_is_prime(a, b):\n",
" ab = int(str(a) + str(b))\n",
" ba = int(str(b) + str(a))\n",
" if is_prime(ab) and is_prime(ba):\n",
" return True\n",
" else:\n",
" return False\n",
" \n",
"assert(concatentation_is_prime(673, 3))\n",
"assert(concatentation_is_prime(673, 1) == False)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[13, 5197, 5701, 6733, 8389]\n",
"26033\n"
]
}
],
"source": [
"potentials = []\n",
"new_potentials = []\n",
"done = False\n",
"for p in primes:\n",
" if done:\n",
" break\n",
" potentials += new_potentials\n",
" new_potentials = []\n",
" for potential in potentials:\n",
" all_concatenations_prime = True\n",
" for prime in potential:\n",
" if not concatentation_is_prime(p, prime):\n",
" all_concatenations_prime = False\n",
" break\n",
" if all_concatenations_prime:\n",
" new_potential = list(potential)\n",
" new_potential.append(p)\n",
" if len(new_potential) > 4:\n",
" print(new_potential)\n",
" s = sum(new_potential)\n",
" done = True\n",
" break\n",
" new_potentials.append(new_potential)\n",
" if p < 15:\n",
" potentials.append([p])\n",
"\n",
"print(s)\n",
"assert(s == 26033)"
]
},
2018-12-24 16:57:03 +01:00
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
2019-01-06 06:50:37 +01:00
"completion_date": "Sun, 6 Jan 2019, 05:00",
2018-12-24 16:57:03 +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"
},
2019-01-06 06:50:37 +01:00
"tags": [
"prime",
"pairs"
]
2018-12-24 16:57:03 +01:00
},
"nbformat": 4,
"nbformat_minor": 2
}