Python Implementation of Problem 32

View source code here on GitHub!

Includes

Problem Solution

Project Euler Problem 32

Once I found out where the end was, it seemed to be relatively easy

Problem:

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.

python.src.p0032.main() int
 1"""
 2Project Euler Problem 32
 3
 4Once I found out where the end was, it seemed to be relatively easy
 5
 6Problem:
 7
 8We shall say that an n-digit number is pandigital if it makes use of all the
 9digits 1 to n exactly once; for example, the 5-digit number, 15234, is 1
10through 5 pandigital.
11
12The product 7254 is unusual, as the identity, 39 × 186 = 7254, containing
13multiplicand, multiplier, and product is 1 through 9 pandigital.
14
15Find the sum of all products whose multiplicand/multiplier/product identity
16can be written as a 1 through 9 pandigital.
17
18HINT: Some products can be obtained in more than one way so be sure to only
19include it once in your sum.
20"""
21from itertools import chain
22
23from .lib.factors import proper_divisors
24from .lib.iters import digits
25
26
27def main() -> int:
28    answer = 0
29    list_of_digits = list(range(1, 10))
30    for product in range(1000, 10**4):
31        for factor in proper_divisors(product):
32            multiplicand = product // factor
33            covered_digits = tuple(
34                chain(digits(factor), digits(multiplicand), digits(product))
35            )
36            if len(covered_digits) != 9:
37                continue
38            elif sorted(covered_digits) == list_of_digits:
39                answer += product
40                break
41    return answer

Tags: pandigital, python-iterator, divisibility