eulerbooks/notebooks/problem0038.ipynb

130 lines
2.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "d5ba1893",
"metadata": {},
"source": [
"# [Pandigital Multiples](https://projecteuler.net/problem=38)\n",
"\n",
"Any number with five or more digits will have a concatenated product with more than 9 digits. Therefore we only need to check numbers with less than five digits."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "ffea7112",
"metadata": {},
"outputs": [],
"source": [
"from itertools import count\n",
"\n",
"def is_pandigital(s):\n",
" return ''.join(sorted(s)) == \"123456789\"\n",
"\n",
"\n",
"pandigitals = dict()\n",
"for k in range(1, 10000):\n",
" s = \"\"\n",
" for n in count(1):\n",
" s += str(k * n)\n",
" if len(s) >= 9:\n",
" break\n",
" \n",
" if is_pandigital(s):\n",
" pandigitals[k] = int(s)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6fe58cf9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1: 123456789,\n",
" 9: 918273645,\n",
" 192: 192384576,\n",
" 219: 219438657,\n",
" 273: 273546819,\n",
" 327: 327654981,\n",
" 6729: 672913458,\n",
" 6792: 679213584,\n",
" 6927: 692713854,\n",
" 7269: 726914538,\n",
" 7293: 729314586,\n",
" 7329: 732914658,\n",
" 7692: 769215384,\n",
" 7923: 792315846,\n",
" 7932: 793215864,\n",
" 9267: 926718534,\n",
" 9273: 927318546,\n",
" 9327: 932718654}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pandigitals"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "faf514b8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"932718654"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"max(pandigitals.values())"
]
},
{
"cell_type": "markdown",
"id": "094c440b",
"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
}