Teaser 2811: Making arrangements
From The Sunday Times, 7th August 2016 [link] [link]
Beth wrote down a three-figure number and she also listed the five other three-figure numbers that could be made using those same three digits. Then she added up the six numbers: it gave a total whose digits were all different, and none of those digits appeared in her original number.
If you knew whether her original number was prime or not, and you knew whether the sum of the three digits of her original number was prime or not, then it would be possible to work out her number.
What was it?
[teaser2811]
Jim Randell 1:47 pm on 17 August 2021 Permalink |
If the number is ABC, then in order for the different orderings of (A, B, C) to make 6 different numbers A, B, C must all be distinct and non-zero. Then we have:
We can use two of the routines from the enigma.py library to solve this puzzle. [[
SubstitutedExpression()]] to solve the alphametic problem, and [[filter_unique()]] to find the required unique solutions.The following Python program runs in 53ms.
Run: [ @replit ]
from enigma import (SubstitutedExpression, filter_unique, unpack, printf) # the alphametic puzzle p = SubstitutedExpression( ["222 * (A + B + C) = PQRS"], d2i={ 0: 'ABCP' }, answer="(ABC, is_prime(ABC), is_prime(A + B + C))", ) # if you knew (f1, f2) you could deduce n rs = filter_unique( p.answers(verbose=0), unpack(lambda n, f1, f2: (f1, f2)), unpack(lambda n, f1, f2: n), ).unique # output solution for (n, f1, f2) in rs: printf("number = {n} (prime = {f1}; dsum prime = {f2})")Solution: Beth’s number was 257.
The only values for (A, B, C) that work are (2, 5, 7) and (3, 7, 9). These can be assembled in any order to give an original number that works.
Of these 257 is the only arrangement that gives a prime number, with a digit sum that is not prime.
LikeLike
GeoffR 9:39 am on 18 August 2021 Permalink |
I used four lists for the two primality tests for each potential 3-digit answer. The list with a single entry was Beth’s number.
from itertools import product from enigma import is_prime # Four lists for primality tests TT, FT, FF, TF = [], [], [], [] # Form six 3-digit numbers - Beth's number was abc for a,b,c in product(range(1,10), repeat=3): abc, acb = 100*a + 10*b + c, 100*a + 10*c + b bac, bca = 100*b + 10*a + c, 100*b + 10*c + a cab, cba = 100*c + 10*a + b, 100*c + 10*b + a pqrs = abc + acb + bac + bca + cab + cba # find four digits of the sum of six numbers p, q = pqrs//1000, pqrs//100 % 10 r, s = pqrs//10 % 10, pqrs % 10 # all seven digits are different if len(set((a, b, c, p, q, r, s))) == 7: # check the primality of number abc # and primality of the sum of its digits if is_prime(abc) and is_prime(a+b+c): TT.append(abc) # true/true if not(is_prime(abc)) and is_prime(a+b+c): FT.append(abc) # false/true if not(is_prime(abc)) and not(is_prime(a+b+c)): FF.append(abc) # false/false if is_prime(abc) and not(is_prime(a+b+c)): TF.append(abc) # true/false # find a single number in the four lists for L in (TT, FT, FF, TF): if len(L) == 1: print(f"Beth's number was {L[0]}.") print("True/True numbers = ", TT) print("False/True numbers = ", FT) print("False/False numbers = ", FF) print("True/False numbers = ", TF) # Beth's number was 257. # True/True numbers = [379, 397, 739, 937] # False/True numbers = [793, 973] # False/False numbers = [275, 527, 572, 725, 752] # True/False numbers = [257]LikeLike
Frits 3:02 pm on 18 August 2021 Permalink |
from itertools import permutations as perm from functools import reduce from enigma import is_prime # convert digits sequence to number d2n = lambda s: reduce(lambda x, y: 10 * x + y, s) # decompose number <t> into <k> increasing numbers from <ns> # so that sum(<k> numbers) equals <t> def decompose(t, k, ns, s=[]): if k == 1: if t in ns and t > s[-1]: yield s + [t] else: for n in ns: if s and n <= s[-1]: continue yield from decompose(t - n, k - 1, ns, s + [n]) # 5 < a + b + c < 25 for sumabc in range(6, 25): # pqrs = abc + acb + bac + bca + cab + cba pqrs = str(222 * sumabc) # skip if duplicate digits if len(set(pqrs)) != len(pqrs): continue # determine minimum and maximum for a, b, c mi = max(1, sumabc - 9 - 8) ma = min(9, sumabc - 1 - 2) # determine digits not used in sum (must be valid for a, b, c) missing = [x for x in range(1, 10) if str(x) not in pqrs and mi <= x <= ma] if len(missing) < 3: continue # check if 3 digits of missing sum to <sumabc> for d in decompose(sumabc, 3, missing): if not is_prime(sumabc): # check which permutatons of a, b, c are prime primes = [n for p in perm(d) if is_prime(n := d2n(p))] if len(primes) == 1: print("answer:", primes[0]) else: # sumabc is prime # check which permutatons of a, b, c are non-prime nprimes = [n for p in perm(d) if not is_prime(n := d2n(p))] if len(nprimes) == 1: print("answer:", nprimes[0])LikeLike
GeoffR 3:08 pm on 19 August 2021 Permalink |
An easy solution with standard MiniZinc, although the answer needs to be interpreted from multiple output configuration.
LikeLike