Solved 44 and 45.
This commit is contained in:
@@ -24,6 +24,79 @@
|
||||
"Find the pair of pentagonal numbers, $P_j$ and $P_k$, for which their sum and difference are pentagonal and $D = |P_k − P_j|$ is minimised; what is the value of D?"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def pentagonal(n):\n",
|
||||
" return n * (3 * n - 1) // 2\n",
|
||||
"\n",
|
||||
"assert(pentagonal(1) == 1)\n",
|
||||
"assert(pentagonal(4) == 22)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"n = 10000\n",
|
||||
"p = [pentagonal(n) for n in range(1, n)]\n",
|
||||
"p_set = set(p)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Okay, I was honestly just going for plain brute force here and the first solution that came up was the solution to the problem... Not really happy with that."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"i = 1020, j = 2167, d = 5482660.\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"for i in range(1, n):\n",
|
||||
" for j in range(i + 1, n):\n",
|
||||
" if p[i - 1] + p[j - 1] in p_set and p[j - 1] - p[i - 1] in p_set:\n",
|
||||
" d = pentagonal(j) - pentagonal(i)\n",
|
||||
" s = d\n",
|
||||
" print(\"i = {}, j = {}, d = {}.\".format(i, j, d))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"5482660\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"print(s)\n",
|
||||
"assert(s == 5482660)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
@@ -35,7 +108,7 @@
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"completion_date": "",
|
||||
"completion_date": "Sat, 22 Dec 2018, 22:57",
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python3.6",
|
||||
@@ -53,7 +126,11 @@
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.6.5"
|
||||
},
|
||||
"tags": []
|
||||
"tags": [
|
||||
"pentagonal",
|
||||
"brute force",
|
||||
"improve"
|
||||
]
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
|
||||
Reference in New Issue
Block a user