1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
from itertools import permutations # two digit primes pr = set(x for x in range(11, 99, 2) if all(x % p for p in (3, 5, 7))) # E and R have different numbers of digits since E would equal R # otherwise. If E had two digits (Ee), they would be equal since # Ee = EeR mod 100, making E = e. So for E to be prime it would # have to be 11 making R = 1 and not a prime. So E is a single # digit odd prime and R is two digits. Hence PRIME is at most 9 # digits, making NUMB at most six digits. This means that NUMB # must include 2 one digit primes and 2 two digit primes. Hence # NUMBER and PRIME 9 digits, making P, I and M two digit primes. # E is a single digit odd prime for E in (3, 5, 7): for R in pr: # R is a two digit prime ending in E if R % 10 != E: continue # obtain M (prime) from the low three digits (MmE = ERr) M = 10 * E + R // 10 if M not in pr: continue # q is the second two digit prime in NUMB for q in pr: # form NUB from this prime and two single digit odd primes for N, U, B in permutations(set((3, 5, 7, q)) - set((E,))): # we now have NUMBER number = ''.join(str(x) for x in (N, U, M, B, E, R)) # match up PRIME with NUMBER to find P, r, I and M P, r, I, M = (int(number[i:i+2]) for i in (0, 2, 4, 6)) # we now have all the numbers (and a second value for R) nbrs = (B, E, I, M, N, P, R, U) # check that the two R values are equal, that P and I are # two digit primes, and that we have eight distinct values if r == R and set((P, I)) < pr and len(set(nbrs)) == 8: # output PRIME and the letter values ltrs = 'BEIMNPRU' t = ', '.join(l + ' = ' + str(n) for l, n in zip(ltrs, nbrs)) print(('PRIME = {:s} (' + t + ')').format(number)) |