Sunday Times Teaser 2870 – Lucky Dip
by Andrew Skidmore
Published September 24 2017 (link)
My bank PIN consists of four different digits in decreasing order. I used this PIN to help me choose my six lottery numbers. I wrote down all the two-figure numbers that used two different digits from the PIN. Just six of those numbers were in the range from 10 to 49 and so they were my lottery choices. In fact the sum of the six is a perfect square. If you knew that square it would now be possible to work out my PIN.
What is my PIN?
One Comment
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425from itertools import combinations, permutationsfrom functools import reducefrom collections import defaultdict# collect PINs with the same lottery number square sumss2p = defaultdict(list)# consider all PINs with four digits in decreasing orderfor pin in combinations(range(9, -1, -1), 4):# use the four digits to form all valid two digit lottery numbersl2 = [x for x in (10 * x + y for x, y in permutations(pin, 2)) if 9 < x < 50]# consider those that give six lottery numbersif len(l2) == 6:sm = sum(l2)# ... that sum to give a perfect squareif int(sm ** 0.5) ** 2 == sm:# map perfect squares to PINss2p[sm].append(reduce(lambda x, y: 10 * x + y, pin))# look for a perfect square that maps to a single PINfor _, (pin, *r) in s2p.items():if not r:print(f'PIN = {pin}')