All Euler problems
Project Euler

Super Pandigital Numbers

A positive number is pandigital in base b if it contains all digits from 0 to b-1 when written in base b. An n -super-pandigital number is a number that is simultaneously pandigital in all bases fr...

Source sync Apr 19, 2026
Problem #0571
Level Level 13
Solved By 1,311
Languages C++, Python
Answer 30510390701978
Length 565 words
combinatoricsmodular_arithmeticprobability

Problem Statement

This archive keeps the full statement, math, and original media on the page.

A positive number is pandigital in base \(b\) if it contains all digits from \(0\) to \(b - 1\) at least once when written in base \(b\).

An \(n\)-super-pandigital number is a number that is simultaneously pandigital in all bases from \(2\) to \(n\) inclusively.

For example \(978 = 1111010010_2 = 1100020_3 = 33102_4 = 12403_5\) is the smallest \(5\)-super-pandigital number.

Similarly, \(1093265784\) is the smallest \(10\)-super-pandigital number.

The sum of the \(10\) smallest \(10\)-super-pandigital numbers is \(20319792309\).

Find \(\displaystyle \sum _{n=3}^{10^7}G(n)\).

Problem 571: Super Pandigital Numbers

Mathematical Foundation

Definition 1. A positive integer NN is pandigital in base bb if the multiset of digits in the base-bb representation of NN contains every element of {0,1,,b1}\{0, 1, \ldots, b-1\} at least once.

Theorem 1 (Minimum digit count). If NN is pandigital in base bb, then NN has at least bb digits in base bb, and consequently Nbb1N \geq b^{b-1}.

Proof. The base-bb representation of NN must include each of the bb distinct symbols {0,1,,b1}\{0, 1, \ldots, b-1\} at least once. Hence the representation has at least bb digits. Since N>0N > 0, the leading digit is nonzero, so N1bb1=bb1N \geq 1 \cdot b^{b-1} = b^{b-1}. \square

Lemma 1 (Base-12 structure). A 12-super-pandigital number NN that is minimally pandigital in base 12 (i.e., has exactly 12 base-12 digits) satisfies 1211N<121212^{11} \leq N < 12^{12}, and its base-12 representation is a permutation of {0,1,2,,11}\{0, 1, 2, \ldots, 11\} with leading digit 1\geq 1.

Proof. By Theorem 1, NN has at least 12 digits in base 12. Suppose NN has exactly 12 digits. Then 1211N<121212^{11} \leq N < 12^{12}. Each of the 12 required symbols must appear at least once among the 12 digit positions. By the pigeonhole principle, each symbol appears exactly once, so the digit string is a permutation of {0,1,,11}\{0, 1, \ldots, 11\}. The leading digit must be nonzero since N1211>0N \geq 12^{11} > 0. \square

Remark. We restrict the search to exactly-12-digit base-12 numbers. A number with 13 or more base-12 digits would satisfy N12128.9×1012N \geq 12^{12} \approx 8.9 \times 10^{12}, which is far larger than the smallest 12-super-pandigital numbers (which turn out to be around 1.8×10111.8 \times 10^{11}). Hence the 10 smallest are all 12-digit base-12 numbers.

Lemma 2 (Bitmask pandigitality test). A number NN is pandigital in base bb if and only if the set {di:i=0,1,,logbN}\{d_i : i = 0, 1, \ldots, \lfloor \log_b N \rfloor\} equals {0,1,,b1}\{0, 1, \ldots, b-1\}, where di=N/bimodbd_i = \lfloor N / b^i \rfloor \bmod b. This can be tested in O(logbN)O(\log_b N) time using a bb-bit mask.

Proof. The digits of NN in base bb are precisely di=N/bimodbd_i = \lfloor N / b^i \rfloor \bmod b for i=0,1,,logbNi = 0, 1, \ldots, \lfloor \log_b N \rfloor. Maintain a bitmask mm (initially 00) where bit jj is set when digit jj is encountered: mm(1j)m \leftarrow m \mathbin{|} (1 \ll j). After processing all digits, NN is pandigital in base bb if and only if m=2b1m = 2^b - 1. Each digit extraction and bitmask update takes O(1)O(1) time, and there are logbN+1\lfloor \log_b N \rfloor + 1 digits. \square

Proposition 1 (Early termination ordering). When checking pandigitality across bases 2,3,,112, 3, \ldots, 11, testing base 11 first maximizes the rejection rate. A random 12-digit base-12 permutation is pandigital in base 11 with probability approximately 11!/11111.4×10311!/11^{11} \approx 1.4 \times 10^{-3}, so the vast majority of candidates are rejected at the first check.

Proof. A base-12 number with 12 digits has approximately 12log12/log11=13\lceil 12 \log 12 / \log 11 \rceil = 13 digits in base 11. Pandigitality in base 11 requires all 11 symbols among these 13\sim 13 digits, and the fraction of such arrangements is small. More precisely, by inclusion-exclusion on missing symbols, the probability of pandigitality in base 11 is k=011(1)k(11k)(1k/11)13\sum_{k=0}^{11} (-1)^k \binom{11}{k} (1 - k/11)^{13}, which evaluates to roughly 0.00140.0014. \square

Editorial

We iterate over each permutation P of remaining. Finally, iterate over b from 11 down to 2. Candidates are generated from the derived formulas, filtered by the required conditions, and processed in order until the desired value is obtained.

Pseudocode

for each permutation P of remaining
for b from 11 down to 2

Complexity Analysis

  • Time. The outer loop iterates over at most 1111!11 \cdot 11! permutations (4.4×108\approx 4.4 \times 10^8). For each candidate, the base-11 pandigitality check costs O(log11N)=O(13)O(\log_{11} N) = O(13) digit extractions. With the empirical rejection rate of 99.86%\sim 99.86\% at base 11, only 6×105\sim 6 \times 10^5 candidates survive to be tested in base 10, and further attrition occurs at each subsequent base. The effective cost is dominated by the base-11 filtering: O(11!×13)5×108O(11! \times 13) \approx 5 \times 10^8 operations for the first digit alone.
  • Space. O(12)O(12) for the permutation and bitmask storage, i.e., O(1)O(1) auxiliary.

Answer

30510390701978\boxed{30510390701978}

Code

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

C++ project_euler/problem_571/solution.cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

bool isPandigital(ll n, int b) {
    int mask = 0, target = (1 << b) - 1;
    while (n > 0) {
        mask |= 1 << (n % b);
        n /= b;
        if (mask == target) return true;
    }
    return mask == target;
}

int main() {
    const int NEED = 10;
    ll pow12[12];
    pow12[0] = 1;
    for (int i = 1; i < 12; i++) pow12[i] = pow12[i - 1] * 12;

    vector<ll> results;

    for (int first = 1; first <= 11; first++) {
        vector<int> rest;
        for (int i = 0; i <= 11; i++)
            if (i != first) rest.push_back(i);
        sort(rest.begin(), rest.end());

        do {
            ll n = (ll)first * pow12[11];
            for (int i = 0; i < 11; i++)
                n += (ll)rest[i] * pow12[10 - i];

            bool ok = true;
            for (int b = 11; b >= 2; b--) {
                if (!isPandigital(n, b)) { ok = false; break; }
            }
            if (ok) {
                results.push_back(n);
                if (first == 1 && (int)results.size() >= NEED) goto done;
            }
        } while (next_permutation(rest.begin(), rest.end()));
    }

done:
    sort(results.begin(), results.end());
    ll total = 0;
    for (int i = 0; i < min((int)results.size(), NEED); i++)
        total += results[i];

    cout << total << endl;
    return 0;
}