output a little more info

This commit is contained in:
filifa 2025-07-20 14:46:09 -04:00
parent d852470da1
commit 34775e69a3
1 changed files with 61 additions and 16 deletions

View File

@ -17,24 +17,13 @@
"execution_count": 1, "execution_count": 1,
"id": "5d61d14d", "id": "5d61d14d",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"data": {
"text/plain": [
"45228"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"def is_pandigital(n):\n", "def is_pandigital(n):\n",
" return ''.join(sorted(n)) == \"123456789\"\n", " return ''.join(sorted(n)) == \"123456789\"\n",
"\n", "\n",
"\n", "\n",
"products = set()\n", "products = dict()\n",
"for p in range(1, 10000):\n", "for p in range(1, 10000):\n",
" for q in range(p, 10000):\n", " for q in range(p, 10000):\n",
" product = p * q\n", " product = p * q\n",
@ -44,9 +33,65 @@
" break\n", " break\n",
" \n", " \n",
" if is_pandigital(s):\n", " if is_pandigital(s):\n",
" products.add(product)\n", " products[(p, q)] = product"
"\n", ]
"sum(products)" },
{
"cell_type": "code",
"execution_count": 2,
"id": "bd9cd5ec",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{(4, 1738): 6952,\n",
" (4, 1963): 7852,\n",
" (12, 483): 5796,\n",
" (18, 297): 5346,\n",
" (27, 198): 5346,\n",
" (28, 157): 4396,\n",
" (39, 186): 7254,\n",
" (42, 138): 5796,\n",
" (48, 159): 7632}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"products"
]
},
{
"cell_type": "markdown",
"id": "3dfd3706",
"metadata": {},
"source": [
"As the problem hinted, some of these products appear more than once, but we only count each once in our sum."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "f63472a3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"45228"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sum(set(products.values()))"
] ]
} }
], ],