Solving Pandigital Multiples with C# and Length‑Based Pruning
In this post, we’ll explore a C# implementation that finds the largest 1–9 pandigital concatenated product by mathematically pruning the search space based on digit lengths, rather than brute-forcing all possibilities.
1. The Pandigital Multiples Problem Recap
We seek the maximum 9‑digit number formed by concatenating the products:
k×1 ‖ k×2 ‖ … ‖ k×n
so that the result uses each digit 1–9 exactly once. Brute‑force tries all k from 1 to 9999, builds and checks each concatenation, but we can do better by analyzing digit counts.
2. Key Insight: Digit‑Length Constraints
- k’s digit length determines how many multipliers
ncan fit into exactly 9 digits.- If
kis 4 digits, thenk×1has 4 digits andk×2has 4 or 5 digits → total ~9. More multipliers overshoot. - If
kis 3 digits, you may needn=3to reach 9 digits (3+3+3).
- If
- For each possible
n, you can solve for the ranges ofkwhose multiplied results exactly fill 9 digits:- Let
d1 = digits(k×1),d2 = digits(k×2), …,dn = digits(k×n). Thend1+…+dn = 9. - Each
diboundskby inequalities like10^{d1−1} ≤ k < 10^{d1}, and similarly fori*k.
- Let
By computing these bounds, we iterate only over valid k ranges for each (n, d1,d2,…), drastically reducing work.
3. C# Implementation Structure
class Program {
static void Main() {
int max = 0, bestK = 0, bestN = 0;
// consider n = 2 and 3 only
for (int n = 2; n <= 3; n++) {
for (int d1 = 1; d1 <= 4; d1++) {
// compute possible kMin/kMax for this (n, d1, …)
// then EvaluateRange(kMin, kMax, n, ref max, ...)
}
}
Console.WriteLine(...);
}
static void EvaluateRange(int kMin, int kMax, int n, ...) {
for (int k = kMin; k <= kMax; k++) {
// build concatenation: string s = k*1 || k*2 || …
// if s.Length == 9 && IsPandigital(s): update max
}
}
static bool IsPandigital(string s) {
return s.Length==9 && s.Distinct().Count()==9 && !s.Contains('0');
}
}
Key functions:
Mainloops overn=2,3, then over possible digit lengthd1ofk×1. Inside, it derivesd2(andd3ifn=3), calculates the lower/upper bounds fork, and callsEvaluateRangeon that tight range.EvaluateRange(kMin,kMax,n,…)iterates only the mathematically validkvalues, concatenates the firstnproducts, and checks pandigitality.IsPandigital(s)ensures the 9-digit string uses each digit 1–9 exactly once.
4. Benefits of Length‑Based Pruning
- Huge reduction in candidate
kvalues: instead of 9 999 tests, you often test only a few hundred or thousand. - Deterministic bounds: by solving
10^{d−1} ≤ k*i < 10^d, we guarantee every consideredkcan produce exactly 9 digits withnmultipliers. - Clarity: the math-driven loops make explicit why each
kis valid, simplifying reasoning and debugging.
5. Performance and Further Tuning
- This C# version runs in a few milliseconds on modern hardware, far outperforming naive brute-force.
- You can further optimize by:
- Avoiding string concatenation inside
EvaluateRange(use integer arithmetic andStringBuilder). - Parallelizing the outer loops (e.g.,
Parallel.Foroverd1orkranges). - Caching digit-length lookups in a small array.
- Avoiding string concatenation inside
6. Conclusion
By leveraging digit-length analysis to prune the search space, this C# implementation efficiently homes in on the few candidate ranges that can yield a 9‑digit concatenated product. It neatly balances mathematical insight with straightforward code, illustrating how a small theoretical observation can yield large practical speedups.

Prev