130 lines
3.2 KiB
Plaintext
130 lines
3.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "004a4666",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Pandigital Products](https://projecteuler.net/problem=32)\n",
|
|
"\n",
|
|
"The product of any number with a four digit number will be at least four digits, meaning it is possible the multiplicand, multiplier, and product are pandigital, since there would be at least nine total digits. In contrast, the product of any number and a five digit number will be at least five digits, so the three-number combo will not be pandigital - it will have at least 11 digits.\n",
|
|
"\n",
|
|
"This allows to set bounds on our brute force. We will make sure neither factor is more than four digits."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "5d61d14d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def is_pandigital(n):\n",
|
|
" return ''.join(sorted(n)) == \"123456789\"\n",
|
|
"\n",
|
|
"\n",
|
|
"products = dict()\n",
|
|
"for p in range(1, 10000):\n",
|
|
" for q in range(p, 10000):\n",
|
|
" product = p * q\n",
|
|
" \n",
|
|
" s = f\"{p}{q}{product}\"\n",
|
|
" if len(s) > 9:\n",
|
|
" break\n",
|
|
" \n",
|
|
" if is_pandigital(s):\n",
|
|
" products[(p, q)] = product"
|
|
]
|
|
},
|
|
{
|
|
"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()))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "04862a57",
|
|
"metadata": {},
|
|
"source": [
|
|
"#### Copyright (C) 2025 filifa\n",
|
|
"\n",
|
|
"This work is licensed under the [Creative Commons Attribution-ShareAlike 4.0 International license](https://creativecommons.org/licenses/by-sa/4.0/) and the [BSD Zero Clause license](https://spdx.org/licenses/0BSD.html)."
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "SageMath 9.5",
|
|
"language": "sage",
|
|
"name": "sagemath"
|
|
},
|
|
"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.11.2"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|