Solved the pandigits, tried to do it way more complicated than necessary at first
This commit is contained in:
53
32/main.py
Normal file
53
32/main.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# 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.
|
||||
|
||||
# So, we only need inputs where the summands are of size 5 and the output of size 4
|
||||
|
||||
from itertools import combinations
|
||||
from itertools import permutations
|
||||
|
||||
def splits(input, left, right):
|
||||
assert len(input) == left + right
|
||||
|
||||
result = []
|
||||
|
||||
combis = combinations(input, left)
|
||||
for c in combis:
|
||||
r = list(set(input) - set(c))
|
||||
result.append((list(c), r))
|
||||
|
||||
return result
|
||||
|
||||
def check(series):
|
||||
result = set()
|
||||
|
||||
for i in range(4):
|
||||
# print("Testing", toInt(series[0:i+1]) , toInt(series[i+1:5]) , toInt(series[5:]))
|
||||
if toInt(series[0:i+1]) * toInt(series[i+1:5]) == toInt(series[5:]):
|
||||
result.add(toInt(series[5:]))
|
||||
|
||||
return result
|
||||
|
||||
def toInt(l):
|
||||
return int("".join(list(map(str,l))))
|
||||
|
||||
def main():
|
||||
print("Hello, this is Patrick")
|
||||
|
||||
digits = range(1, 10)
|
||||
|
||||
perms = permutations(digits)
|
||||
|
||||
products = set()
|
||||
for perm in perms:
|
||||
products.update(check(perm))
|
||||
print(products)
|
||||
|
||||
print(sum(products))
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user