Sunday Times Teaser 2734 – Interlocking Squares
by Andrew Skidmore
Published: 14 February 2015 (link)
Place all of the digits 0 to 9 in the grid so that, reading across or down in crossword fashion, one can see a two-figure square, a three-figure square, and two four-figure squares.
What (in increasing order) are the four squares?
One Comment
Leave one →
-
Brian Gladman permalink1234567891011121314151617181920212223242526272829from itertools import permutations# For the squares with distinct digits in the range a^2 .. (b-1)^2# return a list of tuples holding the square and its set of digitsdef sq(a, b):t = [str(x ** 2) for x in range(a, b)]return [(x, set(x)) for x in t if len(x) == len(set(x))]# lists (as above) for two, three and four digit squaressq2, sq3, sq4 = sq(4, 10), sq(10, 32), sq(32, 100)# pick the two four-digit squares ('a' vertical, 'b' horizontal)for (a, sa), (b, sb) in permutations(sq4, 2):# check that they share only their hundreds digits and# have seven different digits among themif a[1] == b[1] and len(sa | sb) == 7:# pick the three-digit squarefor (c, sc) in sq3:# check that its hundreds digit is the same as b's units digit# and that a, b and c have nine different digits among themif c[0] == b[3] and len((sa | sb | sc)) == 9:# pick the two-digit squarefor (d, sd) in sq2:# check that its unit digit is the same as a's units digit# and that a, b, c and d have ten different digits among themif d[1] == a[3] and len((sa | sb | sc| sd)) == 10:print(*sorted(int(x) for x in (a, b, c, d)))