
Project Euler & HackerRank Problem 32 Solution
Pandigital products
by {BetaProjects} | APRIL 22, 2009 | Project Euler & HackerRankProject Euler Problem 32 Statement
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.
Solution
This is a closed–ended recreational problem, so not much thought was given to a clever solution, but here's a table of results I created before trying to solve the problem:
Total Pandigital Digits | Digits Factor 1 | Digits Factor 2 | Digits Product | Maximum Factor 1 | Maximum Factor 2 | Solutions | Sum of Solutions |
---|---|---|---|---|---|---|---|
4 | 1 | 1 | 2 | 3 | 4 | 3×4=12 | 12 |
5 | 1 | 2 | 2 | 4 | 13 | 4×13=52 | 52 |
6 | 1 | 2 | 3 | 3 | 54 | 3×54=162 | 162 |
7 | None | 0 | |||||
8 | 1 | 3 | 4 | 6 | 582 | 3×582=1746 6×453=2718 | |
8 | 2 | 2 | 4 | 58 | 64 | 24×57=1368 34×52=1768 37×58=2146 58×64=3712 | 13458 |
9 | 1 | 4 | 4 | 4 | 1963 | 4×1738=6952 4×1963=7852 | |
9 | 2 | 3 | 4 | 12 | 483 | 12×483=5796 18×297=5346 27×198=5346 28×157=4396 39×186=7254 42×138=5796 48×159=7632 (Duplicate values are in bold and one was removed for the sum) | 45228 |
An initial solution
print [0,0,0,0,12,52,162,0,13458,45228][input()]
After being accused of breaking the “spirit of Project Euler,” which I have done so may times it should appear as a line item on my resume, I refactored the solution to handle the calculations in real time.
A real–time solution
It's the same little brute–force hack I used to generate the list in the solution above. It runs in about 30ms for any chosen input and little effort in the way of optimization with one small exception. The product can never be more than four digits, so it must always be less than 9999. Except there are no valid factors that could create a product with a nine as the first digit so, I changed it to 8999. Not much, but passes all the HackerRank test cases.
The is_pandigital()
function
The is_pandigital()
function takes a string, n, and the intended length of that string, s, as arguments. The string represents a pandigital candidate with the designated length and is required to pass two tests to qualify as a 1-n pandigital number:
- The length of the string and the intended length are equal.
A literal string, '1234567890', is truncated from the left by the number of characters specified in s and all individual characters in n are stripped from the truncated string. Only when this resultant string is null is the candidate a pandigital number.
For example, if n='12648' and s=5, then the literal '1234567890' is truncated to '12345'. Then, the characters from n, '1', '2', '6', '4', '8', are stripped from '12345' leaving '35'. This would fail the pandigital test. However, a string like '24531' would pass.
HackerRank version
HackerRank Project Euler 32 extends the problem to include all 4–9 digit pandigital sets.
Python Source Code

Last Word
- A Python
set()
was used to collect unique products and eliminate duplicates.