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 →
-
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)