45 lines
1.2 KiB
Python
45 lines
1.2 KiB
Python
#!/usr/bin/env python3
|
||
"""
|
||
Created on 26 Feb 2021
|
||
|
||
@author: David Doblas Jiménez
|
||
@email: daviddoji@pm.me
|
||
|
||
Solution for problem 32 of Project Euler
|
||
https://projecteuler.net/problem=32
|
||
"""
|
||
|
||
from utils import timeit
|
||
|
||
|
||
@timeit("Problem 32")
|
||
def compute():
|
||
"""
|
||
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.
|
||
"""
|
||
|
||
ans = set()
|
||
pandigital = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
|
||
|
||
for x in range(1, 100):
|
||
for y in range(100, 10000):
|
||
# product = x * y
|
||
if sorted(str(x) + str(y) + str(x * y)) == pandigital:
|
||
ans.add(x * y)
|
||
|
||
return sum(ans)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
|
||
print(f"Result for Problem 32: {compute()}") |