## Project Euler & HackerRank Problem 38 Solution

##### Pandigital multiples

by {BetaProjects} | APRIL 23, 2009 | Project Euler & HackerRank### Project Euler Problem 38 Statement

Take the number 192 and multiply it by each of 1, 2, and 3:

192 × 1 = 192

192 × 2 = 384

192 × 3 = 576

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, … , *n*) where *n* > 1?

### Solution

With all the deliberation and justification determining a search range I tried a simple brute force solution to see what I was getting into. But, in 3ms the answer sprinted across the screen and that was that. The program I used is listed in the *repl.it* link.

HackerRank proposed a more extended version that required some thought. But, after submitting a modified brute–force solution it was easily accepted with a run time of 30ms.

Strangely, they set the search limit to 100,000 when it's obvious that only 9,999 will suffice—anything bigger is superfluous. With the search limit capped at 9,999 this solution simply loops through all values of `n` and each multiplier of two through five, concatenating products and checking for pandigitality. The worst case scenario is 40,000 iterations and the benefit is not having to justify any search parameters.

Also, since this is a close-ended problem, no real concern for optimization is required. Once all the numbers are found that make 1-8 and 1-9 concatenated pandigital numbers the universe of possibilities is complete.

#### HackerRank version

HackerRank Project Euler 38 includes either 1-9 or 1-8 pandigital numbers and has you find the multipliers 100 ≤ N ≤ 10^{5} in ascending order.

### Python Source Code

### Last Word

Here is a listing of the solution universe I mentioned earlier generated by the program listed above.

All 1-8 Pandigital solutions18× 1 || 18 × 2 || 18 × 3 || 18 × 4 = 1836547278× 1 || 78 × 2 || 78 × 3 = 781562341728× 1 || 1728 × 2 = 172834561764× 1 || 1764 × 2 = 176435281782× 1 || 1782 × 2 = 178235641827× 1 || 1827 × 2 = 182736542178× 1 || 2178 × 2 = 217843562358× 1 || 2358 × 2 = 235847162718× 1 || 2718 × 2 = 271854362817× 1 || 2817 × 2 = 281756343564× 1 || 3564 × 2 = 356471283582× 1 || 3582 × 2 = 358271644176× 1 || 4176 × 2 = 417683524356× 1 || 4356 × 2 = 43568712 All 1-9 Pandigital solutions9× 1 || 9 × 2 || 9 × 3 || 9 × 4 || 9 × 5 = 918273645192× 1 || 192 × 2 || 192 × 3 = 192384576219× 1 || 219 × 2 || 219 × 3 = 219438657273× 1 || 273 × 2 || 273 × 3 = 273546819327× 1 || 327 × 2 || 327 × 3 = 3276549816729× 1 || 6729 × 2 = 6729134586792× 1 || 6792 × 2 = 6792135846927× 1 || 6927 × 2 = 6927138547269× 1 || 7269 × 2 = 7269145387293× 1 || 7293 × 2 = 7293145867329× 1 || 7329 × 2 = 7329146587692× 1 || 7692 × 2 = 7692153847923× 1 || 7923 × 2 = 7923158467932× 1 || 7932 × 2 = 7932158649267× 1 || 9267 × 2 = 9267185349273× 1 || 9273 × 2 = 9273185469327× 1 || 9327 × 2 = 932718654 ifn> 1 as the problem states, then there are no solutions fork∈ {2,3,4,5,6,7}, wherekis the 1–kpandigital number. The || is the concatenation operator.