All Euler problems
Project Euler

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).

Source sync Apr 19, 2026
Problem #0937
Level Level 37
Solved By 171
Languages C++, Python
Answer 792169346
Length 375 words
number_theorymodular_arithmeticcombinatorics

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 nn is divisible by 9 if and only if the sum of its digits is divisible by 9.

Proof. Write n=i=0mdi10in = \sum_{i=0}^{m} d_i \cdot 10^i. Since 101(mod9)10 \equiv 1 \pmod{9}, we have ndi(mod9)n \equiv \sum d_i \pmod{9}. \square

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 {1,2,,9}\{1, 2, \ldots, 9\}. The digit sum is 1+2++9=451 + 2 + \cdots + 9 = 45, and 9459 \mid 45. By Theorem 1, the number is divisible by 9, hence composite (since it exceeds 9).

An 8-pandigital number uses digits {1,2,,8}\{1, 2, \ldots, 8\}. The digit sum is 1+2++8=361 + 2 + \cdots + 8 = 36, and 9369 \mid 36. The same argument applies. \square

Theorem 3 (Divisibility by 3 test for kk-pandigital). A kk-pandigital number has digit sum k(k+1)/2k(k+1)/2. This is divisible by 3 iff k0k \equiv 0 or k2(mod3)k \equiv 2 \pmod{3}, i.e., for k{2,3,5,6,8,9}k \in \{2, 3, 5, 6, 8, 9\}. Thus kk-pandigital primes can only exist for k{1,4,7}k \in \{1, 4, 7\}.

Proof. k(k+1)/2mod3k(k+1)/2 \bmod 3: for k0(mod3)k \equiv 0 \pmod{3}, k(k+1)/20k(k+1)/2 \equiv 0; for k1k \equiv 1, k(k+1)/21k(k+1)/2 \equiv 1; for k2k \equiv 2, k(k+1)/20k(k+1)/2 \equiv 0. So the digit sum is not divisible by 3 only when k1(mod3)k \equiv 1 \pmod{3}, i.e., k{1,4,7}k \in \{1, 4, 7\}. For other kk, every kk-pandigital number is divisible by 3, hence composite (for k2k \geq 2). \square

Lemma 1 (Primality testing for bounded inputs). For integers below 3.2×10183.2 \times 10^{18}, the deterministic Miller—Rabin test with witnesses {2,3,5,7,11,13,17,19,23,29,31,37}\{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37\} 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 (7654321\leq 7654321), even the weaker result that witnesses {2,3,5,7,11,13}\{2, 3, 5, 7, 11, 13\} suffice below 3.2×10143.2 \times 10^{14} is more than adequate. \square

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: O(k!wlog2n)O(k! \cdot w \cdot \log^2 n) where k!=7!=5040k! = 7! = 5040 is the number of permutations, w=6w = 6 is the number of Miller—Rabin witnesses, and each modular exponentiation takes O(log2n)O(\log^2 n). In the worst case: 5040×6×491.5×1065040 \times 6 \times 49 \approx 1.5 \times 10^6 operations.
  • Space: O(k)O(k) for storing the current permutation.

Answer

792169346\boxed{792169346}

Code

Each problem page includes the exact C++ and Python source files from the local archive.

C++ project_euler/problem_937/solution.cpp
#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;
}