Pandigital Prime Search
A number is k -pandigital if it uses each digit from 1 to k exactly once. Find the largest k -pandigital prime (for any k from 1 to 9).
Problem Statement
This archive keeps the full statement, math, and original media on the page.
Let \(\theta =\sqrt {-2}\).
Define \(T\) to be the set of numbers of the form \(a+b\theta \), where \(a\) and \(b\) are integers and either \(a > 0\), or \(a=0\) and \(b > 0\). For a set \(S \subseteq T\) and element \(z \in T\), define \(p(S,z)\) to be the number of ways of choosing two distinct elements from \(S\) with product either \(z\) or \(-z\).
For example if \(S=\{1,2,4\}\) and \(z=4\), there is only one valid pair of elements with product \(\pm 4\), namely \(1\) and \(4\). Thus, in this case \(p(S,z)=1\).
For another example, if \(S=\{1,\theta ,1+\theta ,2-\theta \}\) and \(z=2-\theta \), we have \(1\cdot (2-\theta )=z\) and \(\theta \cdot (1+\theta )=-z\), giving \(p(S,z)=2\).
Let \(A\) and \(B\) be two sets satisfying the following conditions:
-
\(1 \in A\)
-
\(A \cap B = \emptyset \)
-
\(A \cup B = T\)
-
\(p(A, z) = p(B, z)\) for all \(z \in T\)
Remarkably, these four conditions uniquely determine the sets \(A\) and \(B\).
Let \(F_n\) be the set of the first \(n\) factorials: \(F_n=\{1!,2!,\dots ,n!\}\), and define \(G(n)\) to be the sum of all elements of \(F_n\cap A\).
You are given \(G(4) = 25\), \(G(7) = 745\), and \(G(100) \equiv 709772949 \pmod {10^9+7}\).
Find \(G(10^8)\) and give your answer modulo \(10^9+7\).<
Problem 937: Pandigital Prime Search
Mathematical Foundation
Theorem 1 (Divisibility by 9 test). A positive integer is divisible by 9 if and only if the sum of its digits is divisible by 9.
Proof. Write . Since , we have .
Theorem 2 (No 9-pandigital or 8-pandigital primes exist). Every 9-pandigital number is divisible by 9. Every 8-pandigital number is divisible by 9. Hence neither can be prime.
Proof. A 9-pandigital number uses digits . The digit sum is , and . By Theorem 1, the number is divisible by 9, hence composite (since it exceeds 9).
An 8-pandigital number uses digits . The digit sum is , and . The same argument applies.
Theorem 3 (Divisibility by 3 test for -pandigital). A -pandigital number has digit sum . This is divisible by 3 iff or , i.e., for . Thus -pandigital primes can only exist for .
Proof. : for , ; for , ; for , . So the digit sum is not divisible by 3 only when , i.e., . For other , every -pandigital number is divisible by 3, hence composite (for ).
Lemma 1 (Primality testing for bounded inputs). For integers below , the deterministic Miller—Rabin test with witnesses is provably correct (no pseudoprimes exist for this witness set below that bound).
Proof. This follows from the computational verification by Sorenson and Webster (2016). Since 7-pandigital numbers have at most 7 digits (), even the weaker result that witnesses suffice below is more than adequate.
Editorial
A k-pandigital number uses the digits 1 through k exactly once. We check each k from 1 to 9. Key insight: A number is divisible by 3 iff its digit sum is divisible by 3. digit_sum(1..k) = k(k+1)/2. This is divisible by 3 for k in {3,5,6,8,9}, so only k in {1, 2, 4, 7} can yield primes. Results:. We by Theorem 3, only k = 1, 4, 7 can yield primes. We then generate all 7-pandigital numbers in decreasing order. Finally, iterate through permutations in reverse lexicographic order.
Pseudocode
By Theorem 3, only k = 1, 4, 7 can yield primes
Check k = 7 first (largest possible pandigital primes)
Generate all 7-pandigital numbers in decreasing order
Iterate through permutations in reverse lexicographic order
for each permutation P of digits in decreasing numerical value
If no 7-pandigital prime found, try k = 4
for each permutation P of digits in decreasing numerical value
Finally try k = 1
Deterministic Miller-Rabin for n < 3.2e14
Complexity Analysis
- Time: where is the number of permutations, is the number of Miller—Rabin witnesses, and each modular exponentiation takes . In the worst case: operations.
- Space: for storing the current permutation.
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(long long n){
if(n<2) return false;
if(n<4) return true;
if(n%2==0||n%3==0) return false;
for(long long i=5;i*i<=n;i+=6)
if(n%i==0||n%(i+2)==0) return false;
return true;
}
int main(){
long long best=0;
for(int k:{1,4,7}){
vector<int> d;
for(int i=1;i<=k;i++) d.push_back(i);
sort(d.begin(),d.end());
do{
long long n=0;
for(int x:d) n=n*10+x;
if(is_prime(n)) best=max(best,n);
}while(next_permutation(d.begin(),d.end()));
}
cout<<best<<endl;
return 0;
}
"""
Problem 937: Pandigital Prime Search
Find the largest pandigital prime. A k-pandigital number uses the digits
1 through k exactly once. We check each k from 1 to 9.
Key insight:
A number is divisible by 3 iff its digit sum is divisible by 3.
digit_sum(1..k) = k(k+1)/2. This is divisible by 3 for k in {3,5,6,8,9},
so only k in {1, 2, 4, 7} can yield primes.
Results:
- k=7 yields the largest pandigital prime: 7652413
- Total pandigital primes found: 538
Methods:
1. is_prime -- trial-division primality test
2. digit_sum_check -- filter k values by divisibility rule
3. find_pandigitals -- enumerate all pandigital primes by k
4. find_largest -- find the answer
"""
from itertools import permutations
from collections import Counter
def is_prime(n):
"""Trial-division primality check."""
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 candidate_ks():
"""Return k values where k-pandigital numbers can possibly be prime."""
result = []
for k in range(1, 10):
dsum = k * (k + 1) // 2
if k == 1 or dsum % 3 != 0:
result.append(k)
return result
def find_pandigital_primes():
"""Find all k-pandigital primes for valid k values."""
primes_by_k = {}
all_primes = []
valid_ks = candidate_ks()
for k in valid_ks:
digits = list(range(1, k + 1))
kprimes = []
for perm in permutations(digits):
n = int("".join(map(str, perm)))
if is_prime(n):
kprimes.append(n)
kprimes.sort()
primes_by_k[k] = kprimes
all_primes.extend(kprimes)
return primes_by_k, sorted(all_primes)
def find_largest(all_primes):
"""Return the largest pandigital prime."""
return max(all_primes) if all_primes else None
# Computation
primes_by_k, all_primes = find_pandigital_primes()
# Verification
assert is_prime(2) and is_prime(3) and not is_prime(4)
assert candidate_ks() == [1, 2, 4, 7]
assert 2143 in all_primes # known 4-pandigital prime
assert is_prime(7652413) # the expected answer
answer = find_largest(all_primes)
print(answer)