eulerbooks/notebooks/problem0032.ipynb

75 lines
1.9 KiB
Plaintext
Raw Normal View History

2025-04-21 04:23:51 +00:00
{
"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": [
{
"data": {
"text/plain": [
"45228"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def is_pandigital(n):\n",
" return ''.join(sorted(n)) == \"123456789\"\n",
"\n",
"\n",
"products = set()\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.add(product)\n",
"\n",
"sum(products)"
]
}
],
"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
}