This repository was archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem032.py
More file actions
58 lines (44 loc) · 1.39 KB
/
problem032.py
File metadata and controls
58 lines (44 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
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.
"""
from itertools import count
from math import log10
def digits_in(number):
"""
>>> digits_in(1)
1
>>> digits_in(10)
2
>>> digits_in(111)
3
"""
return int(log10(number)) + 1
def main():
"""
>>> main()
45228
"""
base = frozenset(["1", "2", "3", "4", "5", "6", "7", "8", "9"])
products = set([])
for x in range(2, 80):
for y in count(x + 1):
z = x * y
# is the count of digits not above 9
if digits_in(x) + digits_in(y) + digits_in(z) > 9:
# can't be 1 trough 9 pandigital with more than 9 digits
break
# is x, y and z combind pandigital
if base.issubset(str(x) + str(y) + str(z)):
products.add(z)
print(sum(products))
if __name__ == "__main__":
import doctest
doctest.testmod()