Sunday Times Teaser 2652 – Square Meals
by Robin Nayler
A company uses ingredients numbered 1 to 10 to mix five different types of meusli. Each one has a square number of ingredients and the weight of each ingredient used is the square of the ingredient’s numbet. The total weight of each mixture is a perfect square.
Last month one ingredient had run out so only two types of meusli could be produced. This week only one, different, type of meusli could be made because a different ingredient had run out.
What are the ingredient’s numbers for this week’s meusli?
One Comment
Leave one →
-
brian gladman permalink12345678910111213141516171819202122232425262728293031323334353637from itertools import combinations, productfrom collections import defaultdict# a list for the ingredients of possible mixesmixes = []# the number of ingredients usedfor number_used in (4, 9):# find all combinations of this number of ingredientsfor mix in combinations(range(1, 11), number_used):# find the total weight for this mixweight = sum(x * x for x in mix)# a mix is possible if its weight is a perfect squareif round(weight ** 0.5) ** 2 == weight:mixes += [(weight, mix)]print("meusli mixes weight")for weight, mix in sorted(mixes):print('{:>13s} {:5d}'.format(mix, weight))print()# now take out one ingredientd = defaultdict(list)for missing in range(1, 11):# and find which mixes are still possiblecan_make = [m for w, m in mixes if missing not in m]# index on the number of mixes still possibled[len(can_make)] += [(missing, set(can_make))]# for all pairings of missing ingredients alllowing two# mixes with missing ingredients alllowing one mixfor (m2, mixes), (m1, mix) in product(d[2], d[1]):# we know that these are mutually exclusiveif m1 != m2 and not mix & mixes:fs = "This week's meusli ingredients are {:s}."print(fs.format(min(mix)))