Problem 32
Pandigital Products We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1 through 5 pandigital. The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing multiplicand, multiplier, and product is 1 through 9 pandigital. Find the sum of all products whose multiplicand/multiplier/product identity can be written as a 1 through 9 pandigital. HINT: Some products can be obtained in more than one way so be sure to only include it once in your sum. Answer: 45228
Implementations
// Authored by: Tim Varley 💘// Assisted-by: Grok Code Fast via Crush 💘 <crush@charm.land>
#include <iostream>#include <string>#include <algorithm>#include <set>
long long pandigital_products() { std::string digits = "123456789"; std::set<long long> products; std::sort(digits.begin(), digits.end()); do { for(int i=1; i<=4; i++) { for(int j=i+1; j<=8; j++) { std::string sa = digits.substr(0,i); std::string sb = digits.substr(i,j-i); std::string sc = digits.substr(j); long long a = std::stoll(sa); long long b = std::stoll(sb); long long c = std::stoll(sc); if(a * b == c) { products.insert(c); } } } } while(std::next_permutation(digits.begin(), digits.end())); long long sum = 0; for(auto p : products) sum += p; return sum;}
#if ! defined UNITTEST_MODEint main(int argc, char const *argv[]) { std::cout << pandigital_products() << std::endl;}#endif // UNITTEST_MODESolution Notes
Mathematical Background
A number is pandigital if it uses each digit from 1 to n exactly once. For n=9, we need to find all triplets (a, b, c) where a × b = c, and the concatenation of a, b, and c uses digits 1-9 exactly once.
This requires finding all ways to split the 9 digits into three groups that form valid multiplication equations, with the constraint that no number starts with zero and all digits are unique.
Algorithm Analysis
The implementations use a brute-force approach with permutations:
- Generate all permutations of digits “123456789”
- For each permutation, try all possible ways to split it into three non-empty parts (a, b, c)
- Convert each part to numbers and check if a × b = c
- Collect unique products in a set to handle duplicates
The C++ implementation uses std::next_permutation for efficiency, while others use recursive generation or nested loops.
Performance Analysis
- Time Complexity: O(9! × k) where 9! = 362,880 and k is the number of split positions tried (typically small, around 10-20). This results in approximately 3-7 million operations, executing in milliseconds.
- Space Complexity: O(9!) for storing all permutations in memory (some implementations), or O(1) additional space beyond the current permutation being processed.
- Execution Time: Very fast (under 1 second) due to the small input size; suitable for interactive applications.
- Scalability: Limited to n=9 due to factorial growth, but optimal for the given constraints.
Key Insights
- Permutations ensure all digit arrangements are considered while maintaining uniqueness
- Multiple ways to split the same digits can yield the same product, hence the need for deduplication
- Leading zeros must be avoided to prevent invalid numbers
- The problem has only a few valid solutions despite the large search space
Educational Value
This problem teaches:
- Permutation generation and combinatorial algorithms
- String manipulation and number parsing in different languages
- The importance of handling edge cases (leading zeros, duplicates)
- How brute-force approaches work efficiently for small, constrained problems
- Pandigital number properties and their applications in recreational mathematics