All Euler problems
Project Euler

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

Source sync Apr 19, 2026
Problem #0032
Level Level 01
Solved By 79,316
Languages C++, Python
Answer 45228
Length 411 words
combinatoricssearchbrute_force

Problem Statement

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

We shall say that an -digit number is pandigital if it makes use of all the digits to exactly once; for example, the -digit number, , is through pandigital.

The product is unusual, as the identity, , containing multiplicand, multiplier, and product is through pandigital.

Find the sum of all products whose multiplicand/multiplier/product identity can be written as a through pandigital.

HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum.

Problem 32: Pandigital Products

Mathematical Development

Definition 1. A triple (a,b,p)Z>03(a, b, p) \in \mathbb{Z}_{>0}^3 with p=abp = a \cdot b is a 1-to-9 pandigital identity if the concatenation of the decimal representations of aa, bb, and pp is a permutation of the string 123456789123456789. Let d(x)d(x) denote the number of decimal digits of xx.

Theorem 1 (Digit-count constraint). If (a,b,p)(a, b, p) is a 1-to-9 pandigital identity with aba \le b, then d(p)=4d(p) = 4 and (d(a),d(b)){(1,4),(2,3)}(d(a), d(b)) \in \{(1, 4),\, (2, 3)\}.

Proof. The pandigital condition requires d(a)+d(b)+d(p)=9d(a) + d(b) + d(p) = 9 with no digit 0 and no repeated digits. Let α=d(a)\alpha = d(a), β=d(b)\beta = d(b), γ=d(p)\gamma = d(p).

Since 10α1a<10α10^{\alpha - 1} \le a < 10^\alpha and 10β1b<10β10^{\beta - 1} \le b < 10^\beta, the product satisfies

10α+β2p<10α+β,10^{\alpha + \beta - 2} \le p < 10^{\alpha + \beta},

whence γ{α+β1,α+β}\gamma \in \{\alpha + \beta - 1,\, \alpha + \beta\}.

Case γ=α+β\gamma = \alpha + \beta: Then α+β+(α+β)=9\alpha + \beta + (\alpha + \beta) = 9, giving α+β=9/2Z\alpha + \beta = 9/2 \notin \mathbb{Z}. Contradiction.

Case γ=α+β1\gamma = \alpha + \beta - 1: Then α+β+(α+β1)=9\alpha + \beta + (\alpha + \beta - 1) = 9, giving α+β=5\alpha + \beta = 5 and γ=4\gamma = 4.

Subject to 1αβ1 \le \alpha \le \beta and α+β=5\alpha + \beta = 5, the admissible pairs are (α,β){(1,4),(2,3)}(\alpha, \beta) \in \{(1, 4),\, (2, 3)\}. \square

Lemma 1 (Search bounds). For case (1,4)(1, 4): a[1,9]a \in [1, 9], b[1234,9876]b \in [1234, 9876]. For case (2,3)(2, 3): a[12,98]a \in [12, 98], b[123,987]b \in [123, 987]. In both cases, pp must satisfy 1000p98761000 \le p \le 9876.

Proof. Since digits are drawn from {1,,9}\{1, \ldots, 9\} without repetition, the smallest kk-digit number is 12k\underbrace{12\ldots k} and the largest is formed from the kk largest digits in decreasing order. The constraint d(p)=4d(p) = 4 with digits in {1,,9}\{1, \ldots, 9\} forces 1000p98761000 \le p \le 9876. The bounds on aa and bb follow analogously. \square

Proposition 1 (Deduplication). The problem requires the sum of distinct products. If (a1,b1,p)(a_1, b_1, p) and (a2,b2,p)(a_2, b_2, p) are both pandigital identities with a1a2a_1 \ne a_2, the product pp is counted once.

Proof. The problem statement specifies “sum of all products,” not “sum over all identities.” \square

Theorem 2 (Exhaustive enumeration). The complete set of 1-to-9 pandigital products is {4396,5346,5796,6952,7254,7632,7852}\{4396, 5346, 5796, 6952, 7254, 7632, 7852\}.

Proof. Enumerate all pairs (a,b)(a, b) in the two cases of Theorem 1. For each pair, compute p=abp = ab and test whether d(p)=4d(p) = 4 and the concatenation of digits of aa, bb, pp is a permutation of {1,,9}\{1, \ldots, 9\}. The test is a constant-time operation (sort 9 characters and compare). The search examines at most 9×8643+87×865<1.6×1059 \times 8643 + 87 \times 865 < 1.6 \times 10^5 pairs, each verified or rejected in O(1)O(1), yielding exactly the seven products listed. \square

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 (a,b)(a,b), we compute the product pp, test whether the concatenation of the decimal representations of aa, bb, and pp 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 O(N)O(N) time and O(1)O(1) space, where N1.6×105N \approx 1.6 \times 10^5 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 P=7|P| = 7 elements, requiring O(1)O(1) auxiliary space. \square

Answer

45228\boxed{45228}

Code

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

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