All Euler problems
Project Euler

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.

Source sync Apr 19, 2026
Problem #0041
Level Level 01
Solved By 75,729
Languages C++, Python
Answer 7652413
Length 586 words
number_theorymodular_arithmeticcombinatorics

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 n{1,2,,9}n \in \{1, 2, \ldots, 9\}, an nn-pandigital number is a positive integer whose decimal representation is a permutation of the digits {1,2,,n}\{1, 2, \ldots, n\}.

Definition 2. The digit sum of the nn-pandigital class is S(n)=k=1nk=n(n+1)2S(n) = \sum_{k=1}^{n} k = \frac{n(n+1)}{2}.

Theoretical Development

Theorem 1 (Divisibility by 3). An nn-pandigital number is divisible by 33 if and only if 3S(n)3 \mid S(n). In particular, every nn-pandigital number is divisible by 33 for n{2,3,5,6,8,9}n \in \{2, 3, 5, 6, 8, 9\}.

Proof. Let NN be an nn-pandigital number with decimal digits a1,a2,,ana_1, a_2, \ldots, a_n. By the standard divisibility rule for 33, we have Ni=1nai(mod3)N \equiv \sum_{i=1}^n a_i \pmod{3}. Since {a1,,an}\{a_1, \ldots, a_n\} is a permutation of {1,,n}\{1, \ldots, n\}, the digit sum equals S(n)=n(n+1)/2S(n) = n(n+1)/2 for every nn-pandigital number. Hence 3N3 \mid N if and only if 3S(n)3 \mid S(n).

We compute S(n)mod3S(n) \bmod 3:

nn123456789
S(n)S(n)136101521283645
S(n)mod3S(n) \bmod 3100100100

For n{2,3,5,6,8,9}n \in \{2,3,5,6,8,9\}, we have 3S(n)3 \mid S(n), so every nn-pandigital number is divisible by 33. Since every nn-pandigital number with n2n \geq 2 exceeds 33, it is composite. \square

Lemma 1. No 11-pandigital prime exists.

Proof. The unique 11-pandigital number is 11, which is not prime by the convention that primes are integers p2p \geq 2 with no positive divisors other than 11 and pp. \square

Theorem 2 (Admissible values of nn). The only values of nn for which an nn-pandigital prime can exist are n=4n = 4 and n=7n = 7.

Proof. By Lemma 1, n=1n = 1 is excluded. By Theorem 1, n{2,3,5,6,8,9}n \in \{2,3,5,6,8,9\} forces every nn-pandigital number to be composite. The remaining candidates are n=4n = 4 and n=7n = 7, for which S(n)1(mod3)S(n) \equiv 1 \pmod{3}. Division by 33 is not automatically forced, so primes may exist among these classes. \square

Remark. Theorem 2 does not guarantee that pandigital primes exist for n=4n = 4 or n=7n = 7; it merely asserts these are the only candidates. Existence is established computationally in Theorem 3.

Theorem 3. The largest pandigital prime is 76524137652413.

Proof. By Theorem 2, it suffices to search among 77-pandigital and 44-pandigital numbers. Since every 77-pandigital number exceeds every 44-pandigital number (the smallest 77-pandigital number is 1234567>43211234567 > 4321), we search 77-pandigital numbers first in lexicographically descending order.

There are 7!=50407! = 5040 permutations of {1,2,3,4,5,6,7}\{1,2,3,4,5,6,7\}. The largest is 76543217654321. We enumerate permutations in descending lexicographic order and apply a primality test to each. The first prime encountered is 76524137652413.

Verification of primality. We have 7652413=2766\lfloor \sqrt{7652413} \rfloor = 2766. By trial division, checking all primes p2766p \leq 2766, we confirm that p7652413p \nmid 7652413 for every such pp. Therefore 76524137652413 is prime.

Since 76524137652413 is the first prime encountered in the descending enumeration of 77-pandigital numbers, it is the largest 77-pandigital prime, and hence the largest pandigital prime overall. \square

Editorial

By the divisibility argument, only 77-pandigital and 44-pandigital numbers need to be considered. We enumerate the corresponding digit permutations in descending lexicographic order, first for {1,2,,7}\{1,2,\ldots,7\} and then for {1,2,3,4}\{1,2,3,4\}, convert each permutation into its integer value, and apply a primality test. Because every 77-pandigital number exceeds every 44-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 O(n!Nmax)O(n! \cdot \sqrt{N_{\max}}) time in the worst case, where n7n \leq 7 and Nmax=7654321N_{\max} = 7654321.

Proof. In the worst case, we enumerate all 7!=50407! = 5040 permutations. For each permutation, constructing the integer takes O(n)O(n) time, and the primality test by trial division requires testing divisors up to Nmax<2767\sqrt{N_{\max}} < 2767, costing O(Nmax)O(\sqrt{N_{\max}}) divisions. The total worst-case time is therefore

O(7!7654321)=O(50402767)O(1.39×107).O(7! \cdot \sqrt{7654321}) = O(5040 \cdot 2767) \approx O(1.39 \times 10^7).

In practice, early termination occurs well before exhausting all permutations. \square

Proposition (Space complexity). The algorithm uses O(n)O(n) auxiliary space, where n7n \leq 7.

Proof. The only data structures are the current permutation (nn digits) and loop variables. \square

Answer

7652413\boxed{7652413}

Code

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

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