Brain-Teaser 803: Three perfect squares
From The Sunday Times, 5th December 1976 [link]
To code his message, the Agent began by writing three words — each word being a number, e.g. FIVE. In doing this, he did not write any single letter of the alphabet more than once.
For each of the letters thus written, he then substituted a digit and, in doing so, he used each of the digits 0 to 9 inclusive once (and once only).
He now had three numbers (all in figures) each of which was a perfect square. By adding these three numbers together, he obtained a total running to five figures.
In place of the digits in this total he now wrote the letters for which each of them had originally been substituted. This gave the letters NONSO.
What were the three perfect squares (in figures)?
This puzzle is included in the book The Sunday Times Book of Brain-Teasers: Book 1 (1980). The puzzle text above is taken from the book.
[teaser803]
Jim Randell 9:54 am on 6 July 2021 Permalink |
The three numbers used (when considered as words) must use 10 different letters between them, and the result is 5 letters long. So each of the numbers has at most 5 letters. But if one were to have 5 letters, that would only leave 5 letters for the remaining 2 numbers, and there are no numbers with 2 (or fewer) letters. So the numbers must have (4, 3, 3) letters.
This Python program collects numbers with fewer than 5 letters, and finds 3 of them that between them use 10 different letters (including the letters of NONSO). We then use the [[
SubstitutedExpression]] solver from the enigma.py library to solve the alphametic sum.It runs in 64ms.
Run: [ @replit ]
from enigma import irange, int2words, subsets, union, SubstitutedExpression, sprintf, printf # collect numbers (as words) with fewer than 5 letters words = list(x.upper() for x in map(int2words, irange(0, 10)) if len(x) < 5 and x.isalpha()) # choose three words for ws in subsets(words, size=3): # check there are 10 different symbols (including NOS) ss = union(ws) if not(len(ss) == 10 and ss.issuperset('NOS')): continue # solve the alphametic puzzle exprs = list(sprintf("is_square({w})") for w in ws) t = sprintf("{ws[0]} + {ws[1]} + {ws[2]} = NONSO") exprs.append(t) p = SubstitutedExpression(exprs, verbose=0) for s in p.solve(): printf("{t} | {s}", s=p.substitute(s, t))Solution: The three squares are: 9025, 784, 361.
These correspond to:
LikeLike
GeoffR 7:28 pm on 6 July 2021 Permalink |
% A Solution in MiniZinc include "globals.mzn"; % sets of three and four digit squares set of int: sq = {n*n | n in 10..99}; % he used each of the digits 0 to 9 inclusive once only var 1..9:A; var 0..9:B; var 0..9:C; var 1..9:D; var 0..9:E; var 0..9:F; var 1..9:G; var 0..9:H; var 0..9:I; var 0..9:J; constraint all_different([A, B, C, D, E, F, G, H, I, J]); % 2 no. 3-digit and 1 no. 4-digit squares - ex Jim's analysis var 100..999:ABC = 100*A + 10*B + C; var 100..999:DEF = 100*D + 10*E + F; var 1000..9999:GHIJ = 1000*G + 100*H + 10*I + J; var 10000..99999:NONSO; % the sum of the three squares constraint ABC in sq /\ DEF in sq /\ GHIJ in sq; constraint NONSO == ABC + DEF + GHIJ; % check digit pattern of NONSO for 'N' and 'O' constraint NONSO div 10000 == NONSO div 100 mod 10 /\ NONSO div 1000 mod 10 == NONSO mod 10; solve satisfy; output ["Three squares are " ++ show(ABC) ++ ", " ++ show(DEF) ++ ", " ++ show(GHIJ) ++ "\n" ++ "NONSO, the 5-digit sum = " ++ show(NONSO)]; % Three squares are 361, 784, 9025 % NONSO, the 5-digit sum = 10170 % ---------- % Three squares are 784, 361, 9025 % NONSO, the 5-digit sum = 10170 % ---------- % ==========LikeLike
Frits 6:08 pm on 7 July 2021 Permalink |
@GeoffR,
It is not directly stated but I think N, O and S are meant to be different digits. It doesn’t matter for the solution.
LikeLike
John Crabtree 10:31 am on 9 July 2021 Permalink |
FIVE and NINE only allow one 3-digit number (TWO), and so FOUR is the four-digit number and must contain 0, and, as F = 8 or 9, so must be 9025, 9604 or 9801.
The sum of the digits of NONSO = 0 mod 9. N = 1. If O = 8, S = 0 which is invalid. If O = 6, S = 4, which is invalid. And so FOUR = 9025, S = 7, ie SIX = 784, which leaves TEN = 361.
LikeLike
GeoffR 12:22 pm on 9 July 2021 Permalink |
# three/four digit squares list sq = [x * x for x in range(10, 100)] digits = set('1234567890') from itertools import permutations # 1st stage permutation for p1 in permutations('1234567890', 3): A, B, C = p1 if A == '0': continue ABC = int(A + B + C) if ABC not in sq: continue # 2nd stage permutation q1 = digits.difference(p1) for p2 in permutations(q1, 3): X, Y, Z = p2 if X == '0': continue XYZ = int(X + Y + Z) if XYZ not in sq: continue # 3rd stage permutation q2 = q1.difference(p2) for p3 in permutations(q2): P, Q, R, S = p3 if P == '0': continue PQRS = int(P + Q + R + S) if PQRS not in sq: continue # form sum of three squares NONSO = ABC + XYZ + PQRS # check digit pattern of NONSO if 10000 < NONSO < 100000: if NONSO // 1000 % 10 == NONSO % 10: # 'O' if NONSO // 10000 == NONSO // 100 % 10: # 'N' print(f"Three Squares are {ABC}, {XYZ}, {PQRS}") # Three Squares are 361, 784, 9025LikeLike