All Euler problems
Project Euler

Pandigital Multiples

For a fixed positive integer x, form the concatenated product of x with (1, 2,..., n) for some n > 1. What is the largest 1-to-9 pandigital 9-digit number achievable?

Source sync Apr 19, 2026
Problem #0038
Level Level 01
Solved By 70,083
Languages C++, Python
Answer 932718654
Length 547 words
brute_forceoptimizationsearch

Problem Statement

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

Take the number $192$ and multiply it by each of $1$, $2$, and $3$: \begin{align} 192 \times 1 &= 192\\ 192 \times 2 &= 384\\ 192 \times 3 &= 576 \end{align} By concatenating each product we get the $1$ to $9$ pandigital, $192384576$. We will call $192384576$ the concatenated product of $192$ and $(1,2,3)$.

The same can be achieved by starting with $9$ and multiplying by $1$, $2$, $3$, $4$, and $5$, giving the pandigital, $918273645$, which is the concatenated product of $9$ and $(1,2,3,4,5)$.

What is the largest $1$ to $9$ pandigital $9$-digit number that can be formed as the concatenated product of an integer with $(1,2, \dots, n)$ where $n \gt 1$?

Problem 38: Pandigital Multiples

Mathematical Development

Definitions

Definition 1. The concatenated product of xx with the tuple (1,2,,n)(1, 2, \ldots, n), denoted CP(x,n)\mathrm{CP}(x, n), is the integer obtained by concatenating the decimal representations of x1,x2,,xnx \cdot 1, x \cdot 2, \ldots, x \cdot n:

CP(x,n)=(x)(2x)(nx),\mathrm{CP}(x, n) = \overline{(x) \| (2x) \| \cdots \| (nx)},

where \| denotes string concatenation interpreted as a decimal numeral.

Definition 2. An integer is 1-to-9 pandigital if its decimal representation consists of the digits {1,2,3,4,5,6,7,8,9}\{1, 2, 3, 4, 5, 6, 7, 8, 9\} each appearing exactly once.

Theoretical Development

Theorem 1 (Digit count constraint). For CP(x,n)\mathrm{CP}(x, n) to be a 9-digit pandigital number with n2n \ge 2, we require

k=1nlog10(kx)+1=9.\sum_{k=1}^{n} \lfloor \log_{10}(kx) \rfloor + 1 = 9.

Proof. The concatenation has exactly k=1nd(kx)\sum_{k=1}^{n} d(kx) digits, where d(m)=log10m+1d(m) = \lfloor \log_{10} m \rfloor + 1. For 1-to-9 pandigitality, this sum must equal 9. \blacksquare

Theorem 2 (Feasible parameter space). The digit count constraint restricts (x,n)(x, n) to finitely many cases. The principal families are:

nnDigit pattern (d(x),d(2x),)(d(x), d(2x), \ldots)Range of xx
2(4,5)(4,\, 5)5000x99995000 \le x \le 9999, with 2x100002x \ge 10000
3(3,3,3)(3,\, 3,\, 3)100x333100 \le x \le 333
4(2,2,2,3)(2,\, 2,\, 2,\, 3)25x3325 \le x \le 33
5(1,2,2,2,2)(1,\, 2,\, 2,\, 2,\, 2)5x95 \le x \le 9
9(1,1,,1)(1,\, 1,\, \ldots,\, 1)x=1x = 1

Proof. For n=2n = 2: d(x)+d(2x)=9d(x) + d(2x) = 9. If d(x)=4d(x) = 4, then x[1000,9999]x \in [1000, 9999]. For d(2x)=5d(2x) = 5 we need 2x100002x \ge 10000, i.e., x5000x \ge 5000. If d(x)=5d(x) = 5 then d(x)5d(x) \ge 5 and d(2x)5d(2x) \ge 5, giving 10>9\ge 10 > 9.

For n=3n = 3: d(x)+d(2x)+d(3x)=9d(x) + d(2x) + d(3x) = 9. The minimum total digit count is 3d(x)3 \cdot d(x) (when no product has more digits than xx). For d(x)=3d(x) = 3: 3x999x3333x \le 999 \Rightarrow x \le 333. For d(x)=2d(x) = 2: the total is at least 6, and we need the remaining products to contribute exactly 3 more digits among two terms, which forces specific ranges.

The remaining cases follow analogously. For n10n \ge 10, even x=1x = 1 produces CP(1,10)=12345678910\mathrm{CP}(1, 10) = 12345678910 with 11 digits >9> 9. \blacksquare

Lemma 1 (Leading-digit maximization). Since CP(x,n)\mathrm{CP}(x, n) begins with xx, maximizing CP(x,n)\mathrm{CP}(x, n) requires maximizing the leading portion, which is xx itself. Thus xx should begin with the digit 9.

Proof. For two 9-digit numbers, the one with the lexicographically larger prefix is numerically larger. Since the first d(x)d(x) digits of CP(x,n)\mathrm{CP}(x, n) are exactly xx, a larger xx (in the same digit-count class) produces a larger leading prefix. \blacksquare

Theorem 3 (Optimality of the n=2n = 2 case). For n=2n = 2 and x[5000,9999]x \in [5000, 9999], the concatenated product is CP(x,2)=x2x\mathrm{CP}(x, 2) = \overline{x \| 2x}, a 9-digit number. The maximum pandigital value arises at x=9327x = 9327.

Proof. By Lemma 1, restrict to xx starting with 9, i.e., x[9000,9999]x \in [9000, 9999] with 2x100002x \ge 10000 (so x5000x \ge 5000, satisfied). The 9-digit number CP(x,2)\mathrm{CP}(x, 2) starts with "99\ldots", already larger than any pandigital from other (n,x)(n, x) families (the best n=5n = 5 case gives CP(9,5)=918273645\mathrm{CP}(9, 5) = 918273645 starting with “91…”, and any n=3n = 3 case has x333x \le 333, producing numbers starting with at most “3…”).

Within x[9000,9999]x \in [9000, 9999]: we search for pandigitality. At x=9327x = 9327:

CP(9327,2)=932718654=932718654.\mathrm{CP}(9327, 2) = \overline{9327 \| 18654} = 932718654.

The digit set is {9,3,2,7,1,8,6,5,4}={1,2,3,4,5,6,7,8,9}\{9,3,2,7,1,8,6,5,4\} = \{1,2,3,4,5,6,7,8,9\}: pandigital.

To verify maximality, observe that for x>9327x > 9327 starting with “93”: x[9328,9399]x \in [9328, 9399] can be checked — each fails pandigitality due to repeated digits. For xx starting with “94” through “98”: x9400x \ge 9400 gives 2x188002x \ge 18800, and the first two digits “94…” already constrain the remaining digits; exhaustive verification shows no pandigital result exceeds 932718654932718654. For xx starting with “99”: 2x2x starts with “198” or “199”, repeating 9. \blacksquare

Editorial

We enumerate every possible base integer xx for which the concatenated product could still fit within nine digits, namely 1x99991 \le x \le 9999. For each xx, we append the products x,2x,3x,x, 2x, 3x, \ldots until the concatenation has at least nine digits, and we keep the value only when the concatenation has exactly nine digits, uses each of the digits 1 through 9 exactly once, and involves at least two factors. The maximum such value is the answer.

Pseudocode

Algorithm: Largest Pandigital Concatenated Product
Require: The decimal digits 1 through 9.
Ensure: The largest 1-to-9 pandigital number that can be written as the concatenated product of an integer with (1, 2, ..., n).
1: Initialize best ← 0.
2: For each base x in {1, 2, ..., 9999}, initialize an empty concatenation C and a multiplier n ← 1.
3: While the length of C is less than 9, append the decimal digits of n · x to C and update n ← n + 1.
4: If C has length 9 and uses each digit 1 through 9 exactly once, update best ← max(best, value(C)).
5: Return best.

Complexity Analysis

Proposition. The algorithm runs in O(Xnmax)O(X \cdot n_{\max}) time and O(1)O(1) auxiliary space, where X9999X \le 9999 and nmax9n_{\max} \le 9.

Proof. The outer loop iterates at most 99999999 times. For each xx, the inner loop performs at most 9 concatenations (since 9 single-digit products already fill 9 digits). The pandigital check on a 9-character string is O(9)=O(1)O(9) = O(1). Total: O(9999×9)=O(9×104)O(9999 \times 9) = O(9 \times 10^4). Auxiliary space is O(1)O(1) (a 9-character buffer and integer variables). \blacksquare

Answer

932718654\boxed{932718654}

Code

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

C++ project_euler/problem_038/solution.cpp
#include <bits/stdc++.h>
using namespace std;

bool isPandigital(const string& s) {
    if (s.size() != 9) return false;
    int mask = 0;
    for (char c : s) {
        if (c == '0') return false;
        int d = c - '0';
        if (mask & (1 << d)) return false;
        mask |= (1 << d);
    }
    return mask == 0x3FE;
}

int main() {
    long long best = 0;
    for (int x = 1; x <= 9999; x++) {
        string concat;
        for (int n = 1; n <= 9; n++) {
            concat += to_string((long long)x * n);
            if (concat.size() > 9) break;
            if (n >= 2 && concat.size() == 9 && isPandigital(concat))
                best = max(best, stoll(concat));
        }
    }
    cout << best << endl;
    return 0;
}