Teaser 2758: Square dance
From The Sunday Times, 2nd August 2015 [link]
Dancers numbered from 1 to 9 were about to perform a square dance: five were dressed in red and the rest in blue. They stood in a 3-by-3 array with all three dancers in the first row wearing red and all three in another row wearing blue. Their numbers formed a magic square (i.e. the sum of the three numbers in any row, column or diagonal was the same). One of the dancers in red looked around and noted that the sum of the numbers of the four other dancers in red was the same as the sum of the numbers of the four dancers in blue.
One of the dancers in blue was number 8, what were the numbers of the other three dancers in blue?
[teaser2758]
Jim Randell 4:22 pm on 15 October 2020 Permalink |
The Magic Square uses the numbers 1 – 9 so we know that the magic sum is 15, and the central square is 5. (See: Enigma 1680, Enigma 1080).
We can then choose two numbers (other than 5 and 8) for the reds on the top row, and that lets us work out all the remaining squares.
This Python program runs in 46ms.
Run: [ @replit ]
# consider the square: # # a b c # d e f # g h i # # where each latter stands for a number from 1 to 9 # # the magic sum is: s = (1 + 2 + ... + 9) / 3 = 15 # # and e = s / 3 = 5 from enigma import (irange, div, subsets, printf) # the numbers numbers = set(irange(1, 9)) # total sum, magic sum, centre square t = sum(numbers) s = div(t, 3) e = div(s, 3) # generate magic squares # 8 is a blue, so can't be in the first row for (a, b) in subsets(numbers.difference((e, 8)), size=2, select="P"): c = s - (a + b) if c in {8, a, b, e}: continue # fill out the rest of the square i = s - (a + e) f = s - (c + i) d = s - (e + f) g = s - (a + d) h = s - (g + i) # check it uses the numbers 1 - 9 if numbers.difference({a, b, c, d, e, f, g, h, i}): continue # choose the row with mixed colours for row in ((d, e, f), (g, h, i)): # and choose two from that row to be red for red in subsets(row, size=2): if 8 in red: continue # so the reds are... reds = set((a, b, c) + red) # and if the observant red dancer is x... # we need 2(sum(reds) - x) = (t - x) x = 2 * sum(reds) - t if x not in reds: continue # so the blues are... blues = numbers.difference(reds) printf("{a} {b} {c} / {d} {e} {f} / {g} {h} {i}, reds={reds}, x={x}, blues={blues}")Solution: The other dancers wearing blue had numbers 1, 3 and 6.
The dancers are arranged like this:
(or mirrored about a vertical axis).
Dancer number 9 notices that the sum of the other red dancers (2 + 4 + 7 + 5 = 18) is the same as the sum of the blue dancers (1 + 3 + 6 + 8 = 18).
LikeLike
Frits 12:35 pm on 16 October 2020 Permalink |
# consider the square: # # A B C # D E F # G H I # # where each latter stands for a number from 1 to 9 # # the magic sum is: MS = (1 + 2 + ... + 9) / 3 = 15 # # and E = MS / 3 = 5 from enigma import SubstitutedExpression, irange, is_prime p = SubstitutedExpression([ # magic square "A+B+C == MS", "D+E+F == MS", "G+H+I == MS", "A+D+G == MS", "B+E+H == MS", "C+F+I == MS", "A+E+I == MS", "C+E+G == MS", # for red : pick 2 out of 1st row, 2 out of middle row # for blue: pick 1 out of middle row and 3 out of 3rd row "a*A + b*B + c*C + (1-d)*D + (1-e)*E + (1-f)*F == G+H+I + d*D + e*E + f*F", # pick 1 out of middle row "d + e + f == 1", # pick 2 out of first row "a + b + c == 2", ], verbose=0, symbols="ABCDEFGHIMSabcdef", d2i=dict([(0, "ABCDEFGHI")] + [(8, "ABCabcdef")] + [(k, "abcdef") for k in {2,3,4,5,6,7,9}]), answer="A,B,C,D,E,F,G,H,I, a, b, c, d, e, f, d*D + e*E + f*F ", distinct=("ABCDEFGHI"), digits=range(0, 10) ) # Print answers cnt = 0 for (_, ans) in p.solve(): sol = list(ans[6:9]) + [ans[15]] sol.remove(8) print(f"Answer = {sol}\n") print(f"{ans[:3]} a,b,c {ans[9:12]}\n{ans[3:6]} d,e,f {ans[12:15]}\n{ans[6:9]}\n") # Answer = [6, 1, 3] # # (2, 9, 4) a,b,c (1, 0, 1) # (7, 5, 3) d,e,f (0, 0, 1) # (6, 1, 8) # # Answer = [1, 6, 3] # # (4, 9, 2) a,b,c (1, 0, 1) # (3, 5, 7) d,e,f (1, 0, 0) # (8, 1, 6)LikeLike
Jim Randell 11:58 am on 17 October 2020 Permalink |
@Frits: How do we know the mixed row is the second row and not the bottom row?
LikeLike
Frits 2:57 pm on 17 October 2020 Permalink |
You are right, mixed row can be 2nd or 3rd row.
# consider the square: # # A B C # D E F magic sum A + B + C # G H I # # where each latter stands for a number from 1 to 9 # # a, ..., i are bits from enigma import SubstitutedExpression, irange, is_prime p = SubstitutedExpression([ # magic square with magic sum A + B + C "D + E + F == A + B + C", "G + H + I == A + B + C", "A + D + G == A + B + C", "B + E + H == A + B + C", "C + F + I == A + B + C", "A + E + I == A + B + C", "C + E + G == A + B + C", # pick 4 out of 2nd row or 3rd row, including a whole row "d + e + f + g + h + i == 4", "d + e + f == 3 or g + h + i == 3", # pick 2 out of first row for "a + b + c == 2", # for blue: pick 4 out of 2nd row and 3rd row, including a whole row # for red : pick 2 out of 1st row, 2 out of 2nd row or 3rd row # (which is same as: pick 2 out of 1st row plus # 2 times magic sum - the 4 items picked for blue) "(a+2)*A + (b+2)*B + (c+2)*C == 2 * (d*D + e*E + f*F + g*G + h*H + i*I)", # One of the dancers in blue was number 8 "sum([d*D == 8, e*E == 8, f*F == 8, g*G == 8, h*H == 8, i*I == 8]) == 1", ], verbose=0, symbols="ABCDEFGHIMSabcdefghi", d2i=dict([(0, "ABCDEFGHI")] + [(8, "ABCabcdefghi")] + [(k, "abcdefghi") for k in {2,3,4,5,6,7,9}]), answer="A, B, C, D, E, F, G, H, I, a, b, c, d, e, f, g, h, i, \ d*D + e*E + f*F + g*G + h*H + i*I", distinct=("ABCDEFGHI"), reorder=0 ) # Print answers for (_, ans) in p.solve(): blue = [y[0] for (x,y) in enumerate(zip(ans[3:9], ans[12:18])) if y[1] == 1 and y[0] != 8] print(f"Answer = {blue}\n") print(f"{ans[:3]} a,b,c: {ans[9:12]}\n{ans[3:6]} d,e,f: {ans[12:15]}\n" f"{ans[6:9]} g,h,i: {ans[15:18]}\n") # Answer = [3, 1, 6] # # (4, 9, 2) a,b,c: (1, 0, 1) # (3, 5, 7) d,e,f: (1, 0, 0) # (8, 1, 6) g,h,i: (1, 1, 1) # # Answer = [3, 6, 1] # # (2, 9, 4) a,b,c: (1, 0, 1) # (7, 5, 3) d,e,f: (0, 0, 1) # (6, 1, 8) g,h,i: (1, 1, 1)LikeLike
Frits 3:29 pm on 17 October 2020 Permalink |
@Jim,
Instead of the sum() equation I could have used any() where it not for the fact that “any” contains an “a” which is a variable. Is there an easy way to use normal Python functions without having to worry about lowercase variables?
It would be nice if the SubstitutedExpression solver could ignore consecutive lowercase characters as variables.
LikeLike
Jim Randell 4:30 pm on 17 October 2020 Permalink |
@Frits: Yes, you can just include any alphametic words in curly braces. (e.g. see my solution of Teaser 2919).
LikeLike