add problem 32

This commit is contained in:
filifa 2025-04-21 00:23:51 -04:00
parent ac22f6c944
commit b5b0504d6a
1 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,74 @@
{
"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
}