Sunday Times Teaser 2732 – Prime Meat
by Graham Smithers
Published: 1 February 2015 (link)
Mark has recently converted from vegetarianism. John sent him a coded message consisting of a list of prime numbers. Mark found that by systematically replacing each digit by a letter the list became the message
EAT BEEF AT TIMES
IT IS A PRIME MEAT
What number became PRIME?
2 Comments
Leave one →

Brian Gladman permalink12345678910111213141516171819202122232425262728from itertools import permutationsfrom number_theory import Primeswords = ('EAT', 'BEEF', 'AT', 'TIMES', 'IT', 'IS', 'A', 'PRIME', 'MEAT')# since primes above 10 must end in 1, 3, 7 or 9, we know# that E, F, S and T must be a permutation of these digits# so A can only be 2 or 5for A in ('2', '5'):# permute the digits 1, 3, 7 and 9 for E, F, S and Tfor p in permutations('1379'):E, F, S, T = p# check that AT and EAT are primesAT, EAT = int(A + T), int(E + A + T)if AT in Primes() and EAT in Primes():# now permute remaining digits for the remaining lettersfor q in permutations(set('024685').difference((A,))):# create a map from letters to digitsd = dict(zip('AEFSTBIMPR', (A,) + p + q))# B and P are leading digits so cannot be '0'if '0' not in (d['B'], d['P']):# evaluate all the words and check that they are all primeword_vals = tuple(int(''.join(d[c] for c in w)) for w in words)if all(w in Primes() for w in word_vals):print(word_vals[2], 'became PRIME', word_vals)

geoffrounce permalink1234567891011121314151617181920212223242526272829303132333435363738from itertools import permutationsdef isprime(n):for x in range(2, int(n ** 0.5) + 1):if n % x == 0:return Falsereturn Truefor e, a, t in permutations('987654321', 3):A, AT, EAT, = int(a), int(a + t), int(e + a + t)if not (isprime(A) and isprime(AT) and isprime(EAT)):continuedig_left1 = set('9876543210').difference((e, a, t))for i, m, s in permutations(dig_left1, 3):if '0' in (i, m):continueIS, IT = int(i + s), int(i + t)if not (isprime(IS) and isprime(IT)):continueTIMES, MEAT = int(t + i + m + e + s), int(m + e + a + t)if not (isprime(TIMES) and isprime(MEAT)):continuedig_left2 = dig_left1.difference((i, m, s))for b, p, r, f in permutations(dig_left2):if '0' in (b, p):continueBEEF, PRIME = int(b + e + e + f), int(p + r + i + m + e)if not (isprime(BEEF) and isprime(PRIME)):continueprint('EAT BEEF AT TIMES IT IS A PRIME MEAT')print(EAT, BEEF, AT, TIMES, IT, IS, A, PRIME, MEAT)