Pandigital Prime
We say that an n -digit number is pandigital if it uses each of the digits 1, 2,..., n exactly once. Find the largest n -digit pandigital number that is also prime.
Problem Statement
This archive keeps the full statement, math, and original media on the page.
We shall say that an \(n\)-digit number is pandigital if it makes use of all the digits \(1\) to \(n\) exactly once. For example, \(2143\) is a \(4\)-digit pandigital and is also prime.
What is the largest \(n\)-digit pandigital prime that exists?
Problem 41: Pandigital Prime
Mathematical Development
Definitions
Definition 1. For , an -pandigital number is a positive integer whose decimal representation is a permutation of the digits .
Definition 2. The digit sum of the -pandigital class is .
Theoretical Development
Theorem 1 (Divisibility by 3). An -pandigital number is divisible by if and only if . In particular, every -pandigital number is divisible by for .
Proof. Let be an -pandigital number with decimal digits . By the standard divisibility rule for , we have . Since is a permutation of , the digit sum equals for every -pandigital number. Hence if and only if .
We compute :
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | 3 | 6 | 10 | 15 | 21 | 28 | 36 | 45 | |
| 1 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 0 |
For , we have , so every -pandigital number is divisible by . Since every -pandigital number with exceeds , it is composite.
Lemma 1. No -pandigital prime exists.
Proof. The unique -pandigital number is , which is not prime by the convention that primes are integers with no positive divisors other than and .
Theorem 2 (Admissible values of ). The only values of for which an -pandigital prime can exist are and .
Proof. By Lemma 1, is excluded. By Theorem 1, forces every -pandigital number to be composite. The remaining candidates are and , for which . Division by is not automatically forced, so primes may exist among these classes.
Remark. Theorem 2 does not guarantee that pandigital primes exist for or ; it merely asserts these are the only candidates. Existence is established computationally in Theorem 3.
Theorem 3. The largest pandigital prime is .
Proof. By Theorem 2, it suffices to search among -pandigital and -pandigital numbers. Since every -pandigital number exceeds every -pandigital number (the smallest -pandigital number is ), we search -pandigital numbers first in lexicographically descending order.
There are permutations of . The largest is . We enumerate permutations in descending lexicographic order and apply a primality test to each. The first prime encountered is .
Verification of primality. We have . By trial division, checking all primes , we confirm that for every such . Therefore is prime.
Since is the first prime encountered in the descending enumeration of -pandigital numbers, it is the largest -pandigital prime, and hence the largest pandigital prime overall.
Editorial
By the divisibility argument, only -pandigital and -pandigital numbers need to be considered. We enumerate the corresponding digit permutations in descending lexicographic order, first for and then for , convert each permutation into its integer value, and apply a primality test. Because every -pandigital number exceeds every -pandigital number and the enumeration is descending within each class, the first prime encountered is the largest pandigital prime.
Pseudocode
Algorithm: Largest Pandigital Prime
Require: The digit sets {1, 2, 3, 4} and {1, 2, ..., 7}.
Ensure: The largest pandigital prime.
1: For each admissible digit length m in the order 7, then 4, do:
2: Enumerate the m-digit pandigital permutations in descending lexicographic order.
3: Convert each permutation pi to its integer value p.
4: If p is prime, return p.
5: Return failure if no candidate survives.
Complexity Analysis
Proposition (Time complexity). The algorithm terminates in time in the worst case, where and .
Proof. In the worst case, we enumerate all permutations. For each permutation, constructing the integer takes time, and the primality test by trial division requires testing divisors up to , costing divisions. The total worst-case time is therefore
In practice, early termination occurs well before exhausting all permutations.
Proposition (Space complexity). The algorithm uses auxiliary space, where .
Proof. The only data structures are the current permutation ( digits) and loop variables.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
if (n < 2) return false;
if (n < 4) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; (long long)i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) return false;
}
return true;
}
int main() {
// By Theorem 2, only n=7 and n=4 can yield pandigital primes.
// Search n=7 first in descending lexicographic order.
string digits = "7654321";
do {
int num = stoi(digits);
if (is_prime(num)) {
cout << num << endl;
return 0;
}
} while (prev_permutation(digits.begin(), digits.end()));
// Fallback: search n=4
digits = "4321";
do {
int num = stoi(digits);
if (is_prime(num)) {
cout << num << endl;
return 0;
}
} while (prev_permutation(digits.begin(), digits.end()));
return 0;
}
from itertools import permutations
def is_prime(n):
"""Deterministic primality test by trial division."""
if n < 2:
return False
if n < 4:
return True
if n % 2 == 0 or n % 3 == 0:
return False
i = 5
while i * i <= n:
if n % i == 0 or n % (i + 2) == 0:
return False
i += 6
return True
def solve():
"""Find the largest n-digit pandigital prime.
By the divisibility-by-3 criterion, n-pandigital numbers are composite
for n in {2,3,5,6,8,9}. Only n=7 and n=4 can yield primes.
We search 7-digit pandigital numbers in descending order first.
"""
for n in (7, 4):
for perm in permutations(range(n, 0, -1)):
num = int(''.join(map(str, perm)))
if is_prime(num):
return num
return None
answer = solve()
assert answer == 7652413
print(answer)