# Enigmatic Code

Programming Enigma Puzzles

## Enigma 1680: Do magic

From New Scientist #2847, 14th January 2011 [link]

I have constructed a 3×3 magic square – that is, it contains nine different whole numbers and each row, column and main diagonal has the same sum. But my numbers are in a base other than 10 and I have used letters for the higher “digits”, namely A for 10, B for 11, C for 12, and so on, as far as necessary. The result is that one of the entries in my square now reads as “DO” (the letters D and O) and the bottom row of the square, when read right across, makes the word “MAGIC”.

In decimal notation, (a) What base was I working in? (b) What is the sum of each row?

[enigma1680]

### 5 responses to “Enigma 1680: Do magic”

1. jimrandell 11 January 2012 at 8:20 pm

The following Python program tries all bases up to 36 (when we run out of letters), and finds the solution in 30ms.

```# s0 s1 s2
# s3 s4 s5
# s6 s7 s8

from enigma import irange, printf

digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

(D, O, M, A, G, I, C) = (digits.index(x) for x in list("DOMAGIC"))

# possible splits of MAGIC in base b
def magic(b):
return (
(M, A, G*b*b + I*b + C), # M, A, GIC
(M, A*b + G, I*b + C),   # M, AG, IC
(M, A*b*b + G*b + I, C), # M, AGI, C
(M*b + A, G, I*b + C),   # MA, G, IC
(M*b + A, G*b + I, C),   # MA, GI, C
(M*b*b + A*b + G, I, C)  # MAG, I, C
)

# base needs to be large enough to require O
for b in irange(O + 1, 36):
# possibilities for the bottom row
for (s6, s7, s8) in magic(b):

# determine the magic constant
s = s6 + s7 + s8

# s4 (the centre square) is s/3
(s4, r) = divmod(s, 3)
if r > 0: continue

# and the other squares follow
s0 = s - (s4 + s8)
s1 = s - (s4 + s7)
s2 = s - (s4 + s6)
s3 = s - (s0 + s6)
s5 = s - (s2 + s8)

# they all must be different
sq = set((s0, s1, s2, s3, s4, s5))
if len(sq) != 6: continue

# one of them must be DO
DO = D*b + O
if not(DO in sq): continue

# and they all must be positive integers
if any(not(x > 0) for x in sq): continue

printf("base={b} sum={s} [{s0} {s1} {s2} {s3} {s4} {s5} {s6} {s7} {s8}]")
```

The magic square is (in base 10 and base 34):

```308  916  174      92  QW  54
332  466  600      9Q  DO  HM
758   16  624      MA   G  IC
```

Solution: (a) base 34, (b) 1398.

2. RB 22 January 2012 at 7:22 pm

Jim, I guess you’re solution was a lucky shot. Where did you get the s4=s/3 from? By coincidence, this is the case, but if it hadn’t, you’re solver would not have worked…

• jimrandell 22 January 2012 at 7:52 pm

It’s a property of 3×3 magic squares. You can solve the equations yourself to prove it, or you can get SymPy to do it for you:

```% python
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
>>> from sympy import solve, symbols
>>> (a, b, c, d, e, f, g, h, i, s) = symbols("a b c d e f g h i s")
>>> solve((a+b+c-s, d+e+f-s, g+h+i-s, a+d+g-s, b+e+h-s, c+f+i-s, a+e+i-s, c+e+g-s))[e]
s/3
```
• jimrandell 29 January 2012 at 2:17 pm

Consider the three non-horizontal lines that go through the centre square, as they are magic lines they all sum to s, so:

3s = (a+e+i) + (b+e+h) + (c+e+g)

rearranging gives:

3s = (a+b+c) + (g+h+i) + 3e

since the bracketed terms are also magic lines, each one sums to s, hence:

s = 3e

3. RB 2 February 2012 at 7:42 pm

I see, awesome.

This site uses Akismet to reduce spam. Learn how your comment data is processed.