Sunday Times Teaser 2583 – Playing Cards
by Graham Smithers
Published: 25 March 2012 (link)
Oliver wrote down some numbers and then used a letter-for-digit code to make them into words. In this way his numbers became JACK, QUEEN, KING and ACE. In fact JACK and KING were perfect squares, QUEEN was odd, and ACE was a prime. Furthermore, appropriately enough, the sum of the four was divisible by 52.
What number was represented by QUEEN?
One Comment
Leave one →
-
Brian Gladman permalink12345678910111213141516171819202122232425262728293031from itertools import permutationsfrom functools import reducefrom number_theory import is_prime# four digit perfect squaressquares = { x ** 2:x for x in range(32, 100) }fs = 'QUEEN = {1:d} (JACK = {0:d}, KING = {2:d}, ACE = {3:d})'fn = lambda x, y: 10 * x + y# JACK is a perfect squarefor JACK in squares:J, A, C, K = map(int, str(JACK))# and so is KINGfor KING in squares:_, I, N, G = map(int, str(KING))# find unused digit valuesrest = set(range(10)).difference([A, C, G, I, J, K, N])# check that the two 'K' are the same value, that we have# seven different values and that N is odd (QUEEN is odd)if _ == K and len(rest) == 3 and N % 2:# permute the remaining digits for the reemaining lettersfor Q, U, E in permutations(rest):QUEEN = reduce(fn, (Q, U, E, E, N))ACE = reduce(fn, (A, C, E))# ACE is prime and the sum of JACK, QUEEN, KING and ACE is# a multiple of 52if is_prime(ACE) and not sum((JACK, QUEEN, KING, ACE)) % 52:print(fs.format(JACK, QUEEN, KING, ACE))