138 lines
3.0 KiB
Plaintext
138 lines
3.0 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Pentagon numbers (Euler Problem 44)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {
|
||
"collapsed": true
|
||
},
|
||
"source": [
|
||
"[https://projecteuler.net/problem=44](https://projecteuler.net/problem=44)\n",
|
||
"\n",
|
||
"Pentagonal numbers are generated by the formula, $P_n=n(3n−1)/2$. The first ten pentagonal numbers are:\n",
|
||
"\n",
|
||
" 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...\n",
|
||
"\n",
|
||
"It can be seen that $P_4 + P_7 = 22 + 70 = 92 = P_8$. However, their difference, 70 − 22 = 48, is not pentagonal.\n",
|
||
"\n",
|
||
"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,
|
||
"metadata": {
|
||
"collapsed": true
|
||
},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"metadata": {
|
||
"completion_date": "Sat, 22 Dec 2018, 22:57",
|
||
"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": [
|
||
"pentagonal",
|
||
"brute force",
|
||
"improve"
|
||
]
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 2
|
||
}
|