107 lines
3.0 KiB
Plaintext
107 lines
3.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "004fbd82",
|
|
"metadata": {},
|
|
"source": [
|
|
"# [Pandigital Prime](https://projecteuler.net/problem=41)\n",
|
|
"\n",
|
|
"There's only $1! + 2! + 3! + \\cdots + 9! = 409113$ $n$-digit pandigital numbers. This is a small enough number to brute force, but we can easily optimize even further by applying a [divisibility rule](https://en.wikipedia.org/wiki/Divisibility_rule).\n",
|
|
"\n",
|
|
"If the digits of a number sum to a multiple of 3, that number is divisible by 3. Since:\n",
|
|
"* the digits of every 5-digit pandigital number will sum to 15\n",
|
|
"* the digits of every 6-digit pandigital number will sum to 21\n",
|
|
"* the digits of every 8-digit pandigital number will sum to 36\n",
|
|
"* the digits of every 9-digit pandigital number will sum to 45\n",
|
|
"\n",
|
|
"all of these numbers will be divisible by 3, and therefore not be prime. Consequently, to find the largest $n$-digit pandigital prime, we only need to check 4-digit and 7-digit pandigital numbers."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"id": "9fead48d",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from itertools import permutations\n",
|
|
"\n",
|
|
"pandigitals = set()\n",
|
|
"for n in (4, 7):\n",
|
|
" for permutation in permutations(range(1, n + 1)):\n",
|
|
" k = sum(10^i * d for (i, d) in enumerate(reversed(permutation)))\n",
|
|
" pandigitals.add(k)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "5a4dd5de",
|
|
"metadata": {},
|
|
"source": [
|
|
"Now just sort largest-to-smallest and find the first prime."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"id": "a6eb3473",
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"7652413"
|
|
]
|
|
},
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"for p in reversed(sorted(pandigitals)):\n",
|
|
" if is_prime(p):\n",
|
|
" break\n",
|
|
"\n",
|
|
"p"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "6a9c043d",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Relevant sequences\n",
|
|
"* Pandigital numbers: [A352991](https://oeis.org/A352991)\n",
|
|
"* Pandigital primes: [A216444](https://oeis.org/A216444)\n",
|
|
"\n",
|
|
"#### 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
|
|
}
|