-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathproblem32.py
More file actions
24 lines (20 loc) · 760 Bytes
/
problem32.py
File metadata and controls
24 lines (20 loc) · 760 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 1*2345=2345 <-- using 9 digits
# 12*345=4140 <-- using 9 digits
# 99*999=98901 <-- too many
digits = '123456789'
def generate_perms(nums):
if len(nums) <=1:
yield nums
else:
for perm in generate_perms(nums[1:]):
for i in range(len(perm)+1):
yield perm[:i] + nums[0:1] + perm[i:]
products = []
for perm in generate_perms(digits):
if int(perm[:1]) * int(perm[1:5]) == int(perm[5:]):
print "%d * %d = %d" % (int(perm[:1]), int(perm[1:5]), int(perm[5:]))
products.append(int(perm[5:]))
if int(perm[:2]) * int(perm[2:5]) == int(perm[5:]):
products.append(int(perm[5:]))
print "%d * %d = %d" % (int(perm[:2]), int(perm[2:5]), int(perm[5:]))
print sum(set(products))