Sunday Times Teaser 2882 – Snow White
by Victor Bryant
published December 17 2017 (link)
Snow White placed three balls in a hat (“Oh yes she did!”): written on each ball was a different non-zero digit. She asked one of her little helpers to draw out the three in some order and to use them in that order to make a three-figure number. She knew that this number would be divisible by three but not by seven. She asked the helpers to share out that number of sweets equally amongst the seven of them as far as possible and to give her the remainder. On seeing the remaining sweets she was always able to work out the order in which the three digits had been drawn out.
What (in increasing order) were the three digits?
One Comment
Leave one →
-
Brian Gladman permalink123456789101112131415161718192021from itertools import combinations, permutationsfrom collections import defaultdict# consider all combinations of three non-zero digitsfor c in combinations(range(1, 10), 3):# any number formed from these digits cannot be divisible# by three unless their sum is also divisible by threeif sum(c) % 3 == 0:# find the remainders when the numbers formed from the six# permutations of these three digits are divided by sevenres = {(100 * p[0] + 10 * p[1] + p[2]) % 7 for p in permutations(c)}# there cannot be a zero remainder since Snow White knows# beforehand that any chosen order will not give a number# that is divisible by seven; and since she, knowing the# remainder, can work out which of the six orders of the# balls is drawn, there must be six different remaindersif 0 not in res and len(res) == 6:s, _, e = ', '.join(str(x) for x in c).rpartition(', ')print(f'The three digits were {s} and {e}.')