Solved 61, 62, 63.

This commit is contained in:
2019-01-06 00:50:37 -05:00
parent 3020b89719
commit 550a1479f0
7 changed files with 570 additions and 12 deletions

View File

@@ -37,6 +37,93 @@
"Find the sum of the only ordered set of six cyclic 4-digit numbers for which each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, and octagonal, is represented by a different number in the set."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def get_four_digit_numbers(function):\n",
" r = []\n",
" n = 1\n",
" f = function\n",
" while f(n) < 10000:\n",
" if f(n) > 999:\n",
" r.append(f(n))\n",
" n += 1\n",
" return r\n",
"\n",
"triangles = get_four_digit_numbers(lambda n: n * (n + 1) // 2)\n",
"squares = get_four_digit_numbers(lambda n: n**2)\n",
"pentas = get_four_digit_numbers(lambda n: n * (3 * n - 1) // 2)\n",
"hexas = get_four_digit_numbers(lambda n: n * (2 * n - 1))\n",
"heptas = get_four_digit_numbers(lambda n: n * (5*n - 3) // 2)\n",
"octas = get_four_digit_numbers(lambda n: n * (3*n - 2))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def is_cyclic(a, b):\n",
" return str(a)[-2:] == str(b)[:2]\n",
"\n",
"assert(is_cyclic(3328, 2877))\n",
"assert(is_cyclic(3329, 2877) == False)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[8256, 5625, 2512, 1281, 8128, 2882]\n",
"28684\n"
]
}
],
"source": [
"def search_solution(aggregator, polygonals):\n",
" if not polygonals:\n",
" if is_cyclic(aggregator[-1], aggregator[0]):\n",
" return aggregator\n",
" else:\n",
" return []\n",
"\n",
" if not aggregator:\n",
" for polygonal in polygonals:\n",
" for number in polygonal:\n",
" aggregator.append(number)\n",
" s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n",
" if s:\n",
" return s\n",
" aggregator.pop()\n",
"\n",
" for polygonal in polygonals:\n",
" for number in polygonal:\n",
" if is_cyclic(aggregator[-1], number) and not number in aggregator:\n",
" aggregator.append(number)\n",
" s = search_solution(aggregator, [p for p in polygonals if p != polygonal])\n",
" if s:\n",
" return s\n",
" aggregator.pop()\n",
" return []\n",
"\n",
"s = search_solution([], [triangles, squares, pentas, hexas, heptas, octas])\n",
"print(s)\n",
"s = sum(s)\n",
"print(s)\n",
"assert(s == 28684)"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -48,7 +135,7 @@
}
],
"metadata": {
"completion_date": "",
"completion_date": "Sun, 6 Jan 2019, 05:32",
"kernelspec": {
"display_name": "Python 3",
"language": "python3.6",
@@ -66,7 +153,10 @@
"pygments_lexer": "ipython3",
"version": "3.6.5"
},
"tags": []
"tags": [
"search",
"cyclic"
]
},
"nbformat": 4,
"nbformat_minor": 2