Teaser 3276: First and last multiple
From The Sunday Times, 6th July 2025 [link] [link]
Johann enjoys playing with numbers and making up numerical challenges for his elder brother Jacob. He has worked out a new challenge, which involves Jacob trying to find the whole-number square root of a six-digit number. He explained that the sum of the individual digits [of the six-digit number] is exactly five times the sum of its first and last digits. To make it easier, he tells him that no number is repeated in the six digits, there are no zeros, and the last three digits are in descending numerical order.
What is the square root of the six digit number?
[teaser3276]
Jim Randell 6:48 am on 6 July 2025 Permalink |
Here is a solution using the [[
SubstitutedExpression]] solver from the enigma.py library.It runs in 78ms. (Internal runtime of the generated program is 279µs).
Run: [ @codepad ]
Solution: The square root is 661.
And so the 6-digit number is: 661^2 = 436921.
And we have:
LikeLike
GeoffR 10:17 am on 6 July 2025 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; var 316..999:ans; % square root of abcdef var 100000..999999: abcdef == 100000*a + 10000*b + 1000*c + 100*d + 10*e + f; constraint all_different([a, b, c, d, e, f]); constraint a + b + c + d + e + f == 5 * (a + f); constraint d > e /\ e > f; predicate is_sq(var int: c) = let { var 0..ceil(pow(int2float(ub(c)),(1/2))): z } in z*z = c; constraint ans * ans == abcdef; constraint is_sq(abcdef); solve satisfy; output["Square root of the six digit number = " ++ show(ans)];LikeLike
Frits 3:03 pm on 6 July 2025 Permalink |
# last 2 digits y and z (abcdef = xyz^2) last2 = set(i for i in range(11, 100) if i % 10 and 9 < (m := (i * i) % 100) < 90 and m // 10 > m % 10) for yz in last2: # A + F <= 7 so X < 9 for x in range(3, 9): xyz = 100 * x + yz abcdef = xyz * xyz if len(set(s := str(abcdef))) != 6: continue if s[3] <= s[4] or "0" in s: continue dgts6 = [int(x) for x in s] if sum(dgts6) != 5 * (dgts6[0] + dgts6[-1]): continue print("answer:", xyz)LikeLike
Frits 5:57 pm on 6 July 2025 Permalink |
Processing six-digit numbers to find a valid square root.
from enigma import is_square # last 2 digits e and f (abcdef = xyz^2) last2 = set(divmod(m, 10) for i in range(11, 100) if i % 10 and 9 < (m := (i * i) % 100) < 90 and m // 10 > m % 10) for (e, f) in last2: ef = 10 * e + f for d in range(e + 1, 10): _def = 100 * d + ef # 21 <= 5 * (a + f) <= 39 or 4 < a + f < 8 for a in set(range(max(1, 5 - f), 8 - f))- {d, e, f}: # remaining for b + c if not (3 <= (bc := 5 * (a + f) - (a + d + e + f)) <= 17): continue adef = 10**5 * a + _def # determine missing numbers x and y (x + y = bc with x < y) for x in set(range(max(1, bc - 9), (bc + 1) // 2)) - {a, d, e, f}: if (y := bc - x) in {a, x, d, e, f}: continue for b, c in ((x, y), (y, x)): n = adef + 10**4 * b + 10**3 * c if (xyz := is_square(n)): print("answer:", xyz)LikeLike
Ruud 4:22 pm on 6 July 2025 Permalink |
import math import istr for i in range(int(math.sqrt(100_000)), int(math.sqrt(1_000_000))): if (sum((sq := istr(i * i))) == 5 * (sq[0] + sq[-1])) and sq.all_distinct() and "0" not in sq and sq[-3] > sq[-2] > sq[-1]: print(sq, i)LikeLike
GeoffR 9:17 am on 9 July 2025 Permalink |
sq6 = [ n**2 for n in range(317, 1000) if len(set(str(n**2))) == 6 ] for num in sq6: ds = str(num) if '0' in ds: continue dig = [int(x) for x in ds] if (dig [3] > dig[4] > dig[5]): if sum(dig[:6]) == 5 * (dig[0] + dig[5]): print (f"Square root is {int(num ** 0.5)}.")LikeLike