Solved Euler 50.

main
Felix Martin 2018-12-22 21:45:48 -05:00
parent 584fe0b51b
commit 7898b3e7f9
5 changed files with 23976 additions and 0 deletions

View File

@ -0,0 +1,167 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Consecutive prime sum (Euler Problem 50)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"[https://projecteuler.net/problem=50](https://projecteuler.net/problem=50)\n",
"\n",
"The prime 41, can be written as the sum of six consecutive primes:\n",
"\n",
"$41 = 2 + 3 + 5 + 7 + 11 + 13$\n",
"\n",
"This is the longest sum of consecutive primes that adds to a prime below one-hundred.\n",
"\n",
"The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.\n",
"\n",
"Which prime, below one-million, can be written as the sum of the most consecutive primes?"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def find_max_series(start_index, series_list, series_set): \n",
" series_max = series_list[-1]\n",
" total_max = 0\n",
" total = 0 \n",
" for i in range(start_index, len(series_list)):\n",
" total = total + series_list[i]\n",
" if total in series_set:\n",
" length = i - start_index + 1\n",
" total_max = total\n",
" if total > series_max:\n",
" return (length, total_max)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(6, 41)\n"
]
}
],
"source": [
"n_max = 100\n",
"ps = sieve_of_eratosthenes(n_max)\n",
"ps_set = set(ps)\n",
"ps_max = max(ps)\n",
"s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n",
"print(s)\n",
"assert(s[1] == 41)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(21, 953)\n"
]
}
],
"source": [
"n_max = 1000\n",
"ps = sieve_of_eratosthenes(n_max)\n",
"ps_set = set(ps)\n",
"ps_max = max(ps)\n",
"s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n",
"print(s)\n",
"assert(s[0] == 21)\n",
"assert(s[1] == 953)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(543, 997651)\n",
"997651\n"
]
}
],
"source": [
"n_max = 1000000\n",
"ps = sieve_of_eratosthenes(n_max)\n",
"ps_set = set(ps)\n",
"ps_max = max(ps)\n",
"s = max([x for x in [find_max_series(i, ps, ps_set) for i in range(0, n_max)] if x])\n",
"print(s)\n",
"assert(s[1] == 997651)\n",
"print(s[1])"
]
}
],
"metadata": {
"completion_date": "Sun, 23 Dec 2018, 02:38",
"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": [
"brute force",
"consecutive",
"primes",
"search"
]
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@ -0,0 +1,58 @@
{
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"completion_date": "",
"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": []
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -791,6 +791,34 @@
</tr>
<tr>
<td><a href="EulerProblem050.html">Problem 050</a></td>
<td>Sun, 23 Dec 2018, 02:38</td>
<td>
<kbd>brute force</kbd>
<kbd>consecutive</kbd>
<kbd>primes</kbd>
<kbd>search</kbd>
</td>
</tr>
<tr class="table-warning">
<td><a href="EulerProblem051.html">Problem 051</a></td>
<td></td>
<td>
</td>
</tr>
<tr>
<td><a href="EulerProblem067.html">Problem 067</a></td>