37 lines
941 B
Python
37 lines
941 B
Python
|
|
||
|
def is_concatenated_product_9_pandigital(n):
|
||
|
validation_array = [0 for _ in range(9)]
|
||
|
result = ""
|
||
|
total_length = 0
|
||
|
for i in range(1, 10):
|
||
|
current_product = str(i * n)
|
||
|
for d in current_product:
|
||
|
if d == "0":
|
||
|
return False
|
||
|
index = ord(d) - 49
|
||
|
validation_array[index] += 1
|
||
|
if validation_array[index] > 1:
|
||
|
return False
|
||
|
total_length += len(current_product)
|
||
|
result += current_product
|
||
|
if total_length == 9:
|
||
|
return result
|
||
|
|
||
|
if total_length > 9:
|
||
|
return False
|
||
|
raise Exception("We should not get here.")
|
||
|
|
||
|
|
||
|
def euler_038():
|
||
|
r = []
|
||
|
for i in range(10000):
|
||
|
p = is_concatenated_product_9_pandigital(i)
|
||
|
if p:
|
||
|
r.append(int(p))
|
||
|
return max(r)
|
||
|
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
print("e038.py: " + str(euler_038()))
|
||
|
assert(euler_038() == 932718654)
|