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...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
A positive number is
An \(n\)
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 is pandigital in base if the multiset of digits in the base- representation of contains every element of at least once.
Theorem 1 (Minimum digit count). If is pandigital in base , then has at least digits in base , and consequently .
Proof. The base- representation of must include each of the distinct symbols at least once. Hence the representation has at least digits. Since , the leading digit is nonzero, so .
Lemma 1 (Base-12 structure). A 12-super-pandigital number that is minimally pandigital in base 12 (i.e., has exactly 12 base-12 digits) satisfies , and its base-12 representation is a permutation of with leading digit .
Proof. By Theorem 1, has at least 12 digits in base 12. Suppose has exactly 12 digits. Then . 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 . The leading digit must be nonzero since .
Remark. We restrict the search to exactly-12-digit base-12 numbers. A number with 13 or more base-12 digits would satisfy , which is far larger than the smallest 12-super-pandigital numbers (which turn out to be around ). Hence the 10 smallest are all 12-digit base-12 numbers.
Lemma 2 (Bitmask pandigitality test). A number is pandigital in base if and only if the set equals , where . This can be tested in time using a -bit mask.
Proof. The digits of in base are precisely for . Maintain a bitmask (initially ) where bit is set when digit is encountered: . After processing all digits, is pandigital in base if and only if . Each digit extraction and bitmask update takes time, and there are digits.
Proposition 1 (Early termination ordering). When checking pandigitality across bases , testing base 11 first maximizes the rejection rate. A random 12-digit base-12 permutation is pandigital in base 11 with probability approximately , so the vast majority of candidates are rejected at the first check.
Proof. A base-12 number with 12 digits has approximately digits in base 11. Pandigitality in base 11 requires all 11 symbols among these 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 , which evaluates to roughly .
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 permutations (). For each candidate, the base-11 pandigitality check costs digit extractions. With the empirical rejection rate of at base 11, only 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: operations for the first digit alone.
- Space. for the permutation and bitmask storage, i.e., auxiliary.
Answer
Code
Each problem page includes the exact C++ and Python source files from the local archive.
#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;
}
#!/usr/bin/env python3
"""Project Euler Problem 571: Super Pandigital Numbers
Find the sum of the 10 smallest 12-super-pandigital numbers.
"""
from itertools import permutations
def is_pandigital(n, base):
"""Return True iff n is pandigital in the given base (bitmask test)."""
mask = 0
target = (1 << base) - 1
while n > 0:
mask |= 1 << (n % base)
n //= base
if mask == target:
return True
return mask == target
def solve():
NEED = 10
pow12 = [12 ** i for i in range(12)]
results = []
for first in range(1, 12):
rest = [d for d in range(12) if d != first]
for perm in permutations(rest):
n = first * pow12[11]
for i, d in enumerate(perm):
n += d * pow12[10 - i]
if all(is_pandigital(n, b) for b in range(11, 1, -1)):
results.append(n)
if first == 1 and len(results) >= NEED:
break
results.sort()
print(sum(results[:NEED]))
if __name__ == "__main__":
solve()