Teaser 2684: How safe?
From The Sunday Times, 2nd March 2014 [link] [link]
Fed up with being burgled, George and Martha have bought a new safe. They remember its six-figure combination as three two-figure primes. Martha noted that the product of the six different digits of the combination was a perfect square, as was the sum of the six. George commented further that the six-figure combination was actually a multiple of the difference between that product and sum.
What is the six-figure combination?
[teaser2684]
Jim Randell 3:43 pm on 23 July 2020 Permalink |
If the combination can be considered as three 2-digit primes, then it cannot contain 0 (as a 2-digit prime cannot begin or end with 0).
This Python program looks for 6 different (non-zero) digits that have a square for both their sum and product, and looks for 6-digit multiples of the difference between these that use the same digits, and can be split into three 2-digit prime numbers. It runs in 52ms.
Run: [ @replit ]
from enigma import (subsets, irange, multiply, is_square, divc, is_prime, nsplit, printf) # look for 6 different digits for ds in subsets(irange(1, 9), size=6, select="C"): # the sum and product must both be squares s = sum(ds) p = multiply(ds) if not (is_square(s) and is_square(p)): continue (d, ds) = (p - s, set(ds)) # consider 6-digit multiples of d for n in irange(d * divc(100000, d), 999999, step=d): # does it use the same digits? if ds.difference(nsplit(n)): continue # does it split into primes? if not all(is_prime(p) for p in nsplit(n, base=100)): continue # output solution printf("{n} [s={s} p={p} d={d}]")Solution: The combination is 296143.
There is only one set of 6 different digits that have a square for both their sum and product: 1, 2, 3, 4, 6, 9 (sum = 5², product = 36²). So the combination is a rearrangement of these digits that is a multiple of 1271, that can be split into three 2-digit primes. In fact 296143 is the only multiple of 1271 that uses the required digits, so the prime test is superfluous.
LikeLike
GeoffR 8:38 pm on 23 July 2020 Permalink |
% A Solution in MiniZinc include "globals.mzn"; var 1..9:A; var 1..9:B; var 1..9:C; var 1..9:D; var 1..9:E; var 1..9:F; constraint all_different([A,B,C,D,E,F]); % three two-figure primes var 11..97: AB = 10*A + B; var 11..97: CD = 10*C + D; var 11..97: EF = 10*E + F; var 720..60480: dig_prod = A * B * C * D * E * F; var 21..39: dig_sum = A + B + C + D + E + F; % Safe combination is ABCDEF var 100000..999999: comb = 100000*A + 10000*B + 1000*C + 100*D + 10*E + F; predicate is_prime(var int: x) = x > 1 /\ forall(i in 2..1 + ceil(sqrt(int2float(ub(x))))) ((i < x) -> (x mod i > 0)); predicate is_sq(var int: y) = let { var 1..ceil(sqrt(int2float(ub(y)))): z } in z*z = y; % the combination is split into three 2-digit primes constraint is_prime(AB) /\ is_prime(CD) /\ is_prime(CD); % digit product and digit sum are both squares constraint is_sq(A * B * C * D * E * F); constraint is_sq(A + B + C + D + E + F); % the six-figure combination was actually a multiple of % the difference between that product and sum. constraint comb mod (dig_prod - dig_sum) == 0; solve satisfy; output["Safe combination is " ++ show(comb) ++ "\nDigits product = " ++ show(dig_prod)]; % Safe combination is 296143 % Digits product = 1296 % ---------- % ==========LikeLike
GeoffR 4:44 pm on 24 July 2020 Permalink |
I carried out some further tests to see if there were any constraints which were not essential to get the correct answer.
I found any one of the following MiniZinc constraints could be removed, and the single correct answer obtained:
1) requirement for digits to be all different
2) requirement for three 2-digit primes
3) requirement for sum of digits to be a square
4) requirement product of digits to be a square.
However, I could not remove more than one of these constraints and get the correct answer.
LikeLike