Tim Varley Logo
Tim Varley Systems Engineer
Problem #38 hard

Pandigital Multiples

Take the number 192192 and multiply it by each of 11, 22, and 33:

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$?

Implementations

cpp
// Answer: 932718654
// Authored by: Tim Varley πŸ’˜
// Assisted-by: Grok Code Fast via Crush πŸ’˜ <crush@charm.land>
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
long long pandigital_multiples() {
long long max_pandigital = 0;
for(int x=1; x<10000; x++) {
std::string concat = "";
for(int k=1; ; k++) {
concat += std::to_string(x * k);
if(concat.size() > 9) break;
if(concat.size() == 9) {
std::string check = "123456789";
std::string sorted = concat;
std::sort(sorted.begin(), sorted.end());
if(sorted == check) {
long long num = std::stoll(concat);
if(num > max_pandigital) max_pandigital = num;
}
break;
}
}
}
return max_pandigital;
}
#if ! defined UNITTEST_MODE
int main(int argc, char const *argv[]) {
std::cout << pandigital_multiples() << std::endl;
}
#endif // UNITTEST_MODE

Solution Notes

Mathematical Background

A pandigital number uses each digit from 1 to n exactly once. Here we seek 9-digit pandigital numbers formed by concatenating multiples of a base number.

For example, 192 Γ— (1,2,3) = 192, 384, 576 β†’ 192384576 (pandigital).

We need the largest such number where n > 1 (at least two multiples concatenated).

Algorithm Analysis

The implementations use brute force enumeration:

  • Iterate through possible base numbers (1 to 9999)
  • For each base, concatenate multiples (Γ—1, Γ—2, Γ—3, …) until reaching 9 digits
  • Check if exactly 9 digits and contains each digit 1-9 exactly once
  • Track the maximum valid number found

Pandigital check uses digit counting or set operations.

Performance Analysis

  • Time Complexity: O(U Γ— k) where U=10,000 and k~5 (average concatenations needed), resulting in ~50,000 operations. Executes instantly.
  • Space Complexity: O(1) - only strings for current concatenation.
  • Execution Time: Very fast (milliseconds), suitable for real-time applications.
  • Scalability: Linear in upper bound, but fixed by pandigital constraint.

Key Insights

  • Largest pandigital multiple is 9,327,186,54
  • Base numbers must be chosen so concatenation yields exactly 9 digits
  • Cannot contain zero or repeated digits
  • Starting from higher base numbers finds larger results faster

Educational Value

This problem teaches:

  • String concatenation and manipulation
  • Set operations for uniqueness checking
  • The concept of pandigital numbers
  • Systematic search with constraints
  • Optimization by starting from high values