// you’re reading...

Project Euler Solutions

Project Euler Problem 38 Solution

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Problem Description

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?

Analysis

We need to find a 1 to 9 pandigital number greater than 918273645 mentioned above (otherwise 918273645 would be the answer, and we tried that, and it wasn’t).

[In the following paragraph we are using ? as a wild card definition for a digit [1-9] and the ++ as the concatenation operator.]

Starting with n=2, we can eliminate (9? × 1) ++ (9? × 2) and (9?? × 1) ++ (9?? × 2) because they will never yield a 9 digit number, but (9??? × 1) ++ (9??? × 2) will, so n = 2 and our starting search must be a 4 digit number beginning with 9 and never contain a zero; namely 9123. It should be obvious that any n>2 is impossible.

Solution>

Runs < 1 second in Python.

from Euler import is_pandigital
 
for n in range(9876, 9123, -1):
  p = str(n*1) + str(n*2)
  if is_pandigital(p): 
    print "Answer to PE38 = ", p
    break

Comments

More information on the Euler module can be found on the tools page.

Discussion

No comments for “Project Euler Problem 38 Solution”

Post a comment