Brainteaser 1449: Magic!
From The Sunday Times, 17th June 1990 [link]
This is a magic square containing all the numbers from 1 to 16 once each. As is always the case with magic squares, all rows, columns, and full-length diagonals add up to the same sum (but all our shorter diagonals add to less than that sum).
Fill in the missing numbers.
[teaser1449]

Jim Randell 9:19 am on 30 November 2023 Permalink |
If we consider the four rows that make up the square, each row sums to the magic constant k, and between all 4 rows each number is included exactly once. So:
We can now use the [[
SubstitutedExpression]] solver from the enigma.py library to fill out the remaining squares.The following run file executes in 96ms. (Internal runtime of the generated program is 853µs).
Run: [ @replit ]
Solution: Here is the completed square:
LikeLike
NigelR 6:22 pm on 30 November 2023 Permalink |
Hi Jim
I agree your solution, but I’m not sure how you reached it with your short diagonal condition of
“E + J + P < 34" (which isn't satisfied by the solution). Shouldn't it be E+J+O?
LikeLike
NigelR 6:25 pm on 30 November 2023 Permalink |
Apologies: Just realised that your notation didn’t include ‘O’!!
LikeLike
Jim Randell 7:55 pm on 30 November 2023 Permalink |
@NigelR: I often skip O as a variable, as it is easily confused with 0 (zero).
LikeLike
GeoffR 7:02 pm on 1 December 2023 Permalink |
# A B C D # E F G H # I J K L # M N P Q from itertools import product, permutations from enigma import all_different # Given grid values A, J, N, Q = 9, 13, 12, 14 # Find remaining 12 digits digits = set(range(1,17)).difference({9, 13, 12, 14 }) # 1st row of grid for B, C, D in product(digits, repeat=3): if not all_different(A, B, C, D):continue if A + B + C + D != 34:continue R1 = list(digits.difference({B, C, D})) # 2nd row of grid for E, F, G, H in product(R1, repeat=4): if not all_different(E, F, G, H):continue if E + F + G + H != 34:continue R2 = set(R1).difference({E, F, G, H}) # 3rd row of grid for I, K, L in product(R2, repeat=3): if not all_different(I, J, K, L):continue if I + J + K + L != 34:continue if B + G + L >= 34:continue if C + F + I >= 34:continue # only M and P are missing from the 4th row of grid R3 = list(set(R2).difference({I, K, L})) for M, P in permutations(R3, 2): # columns if A + E + I + M != 34:continue if B + F + J + N != 34:continue if C + G + K + P != 34:continue if D + H + L + Q != 34:continue # diagonals if E + J + P >= 34:continue if H + K + N >= 34:continue if A + F + K + Q != 34:continue if D + J + G + M != 34:continue print('A, B, C, D = ', A,B,C,D) print('E, F, G, H = ', E,F,G,H) print('I, J, K, L = ', I,J,K,L) print('M, N, P, Q = ', M,N,P,Q) exit() # only 1 solution needed # A, B, C, D = 9 6 15 4 # E, F, G, H = 16 3 10 5 # I, J, K, L = 2 13 8 11 # M, N, P, Q = 7 12 1 14LikeLike
Hugo 10:01 am on 2 December 2023 Permalink |
I found a second solution. Did I do something wrong?
9 2 15 8
6 7 10 11
16 13 4 1
3 12 5 14
LikeLike
Jim Randell 11:03 am on 2 December 2023 Permalink |
Yes, the shorter diagonals have to sum less than 34.
You have 16 + 7 + 15 = 38 in your diagram.
LikeLike
Hugo 11:16 am on 2 December 2023 Permalink |
Aha! I misread the condition as “not equal to that sum”.
LikeLike