Pandigital Products
We say that an n -digit number is pandigital if it uses all the digits 1 to n exactly once. Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through...
Problem Statement
This archive keeps the full statement, math, and original media on the page.
We shall say that an
The product
Find the sum of all products whose multiplicand/multiplier/product identity can be written as a
Problem 32: Pandigital Products
Mathematical Development
Definition 1. A triple with is a 1-to-9 pandigital identity if the concatenation of the decimal representations of , , and is a permutation of the string . Let denote the number of decimal digits of .
Theorem 1 (Digit-count constraint). If is a 1-to-9 pandigital identity with , then and .
Proof. The pandigital condition requires with no digit 0 and no repeated digits. Let , , .
Since and , the product satisfies
whence .
Case : Then , giving . Contradiction.
Case : Then , giving and .
Subject to and , the admissible pairs are .
Lemma 1 (Search bounds). For case : , . For case : , . In both cases, must satisfy .
Proof. Since digits are drawn from without repetition, the smallest -digit number is and the largest is formed from the largest digits in decreasing order. The constraint with digits in forces . The bounds on and follow analogously.
Proposition 1 (Deduplication). The problem requires the sum of distinct products. If and are both pandigital identities with , the product is counted once.
Proof. The problem statement specifies “sum of all products,” not “sum over all identities.”
Theorem 2 (Exhaustive enumeration). The complete set of 1-to-9 pandigital products is .
Proof. Enumerate all pairs in the two cases of Theorem 1. For each pair, compute and test whether and the concatenation of digits of , , is a permutation of . The test is a constant-time operation (sort 9 characters and compare). The search examines at most pairs, each verified or rejected in , yielding exactly the seven products listed.
Editorial
We enumerate only the feasible factor-length patterns identified in the mathematical development: a 1-digit multiplicand times a 4-digit multiplier, or a 2-digit multiplicand times a 3-digit multiplier. For each candidate pair , we compute the product , test whether the concatenation of the decimal representations of , , and uses each digit from 1 to 9 exactly once, and insert valid products into a set so that duplicates are counted only once in the final sum.
Pseudocode
Algorithm: Sum of Pandigital Products
Require: The decimal digits 1 through 9.
Ensure: The sum of all products whose multiplicand, multiplier, and product together form a 1-to-9 pandigital identity.
1: Initialize an empty set P of products.
2: For each feasible factor-length pattern, enumerate multiplicands x and multipliers y of the prescribed lengths.
3: Let z ← x · y; if the concatenation of x, y, and z uses each digit 1 through 9 exactly once, insert z into P.
4: Return the sum of all elements of P.
Complexity Analysis
Proposition 2. The algorithm runs in time and space, where is the number of candidate pairs.
Proof. The outer loops iterate over a fixed number of pairs determined by Lemma 1. The pandigital test for each pair examines exactly 9 digits (constant work). The set of products has at most elements, requiring auxiliary space.
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_pandigital(int a, int b, int c) {
int digits[10] = {};
for (int x : {a, b, c})
while (x > 0) { digits[x % 10]++; x /= 10; }
if (digits[0] != 0) return false;
for (int i = 1; i <= 9; i++)
if (digits[i] != 1) return false;
return true;
}
int main() {
set<int> products;
for (int a = 1; a <= 9; a++)
for (int b = 1234; b <= 9876; b++) {
int p = a * b;
if (p >= 1000 && p <= 9999 && is_pandigital(a, b, p))
products.insert(p);
}
for (int a = 12; a <= 98; a++)
for (int b = 123; b <= 987; b++) {
int p = a * b;
if (p >= 1000 && p <= 9999 && is_pandigital(a, b, p))
products.insert(p);
}
int sum = 0;
for (int p : products) sum += p;
cout << sum << endl; // 45228
return 0;
}
"""Project Euler Problem 32: Pandigital Products"""
def is_pandigital(a, b, p):
s = str(a) + str(b) + str(p)
return len(s) == 9 and set(s) == set('123456789')
def main():
products = set()
for a in range(1, 10):
for b in range(1234, 9877):
p = a * b
if 1000 <= p <= 9999 and is_pandigital(a, b, p):
products.add(p)
for a in range(12, 99):
for b in range(123, 988):
p = a * b
if 1000 <= p <= 9999 and is_pandigital(a, b, p):
products.add(p)
print(sum(products)) # 45228
if __name__ == "__main__":
main()