Enigmatic Code

Programming Enigma Puzzles

Enigma 1593: Three squares

From New Scientist #2758, 1st May 2010 [link]

On three identical square cards I wrote 3 × 3 magic squares (where the sums of each row, column and major diagonal are equal) using the numbers 1 to 9 on the first, 10 to 18 on the second and 19 to 27 on the third. I formed them into a sandwich in some order. I then found that I could push a pin through the cards so that it passed through three multiples of three. Furthermore, writing the three numbers in order along the pin gave a five-figure perfect square. One number on the middle card was sandwiched between two perfect squares on the other cards.

Which number?


One response to “Enigma 1593: Three squares

  1. jimrandell 19 January 2012 at 10:45 am

    The following Python program runs in 39ms.

    import itertools
    from enigma import irange, concat, is_square
    # generate all rotations/reflections of 3x3 magic square indices
    def magic():
      s = [(7, 0, 5, 2, 4, 6, 3, 8, 1)] # initial magic square
      # rotations
      for i in irange(0, 2):
        s.append(tuple(s[-1][i] for i in (6, 3, 0, 7, 4, 1, 8, 5, 2)))
      # reflections
      for j in irange(0, 3):
        s.append(tuple(s[j][i] for i in (2, 1, 0, 5, 4, 3, 8, 7, 6)))
      return s
    squares = magic()
    # generate possible magic squares from a list of 9 consecutive numbers
    def generate(l):
      for s in squares:
        yield tuple(l[i] for i in s)
    # generate the possible cards
    for C1 in generate(irange(1, 9)):
      for C2 in generate(irange(10, 18)):
        for C3 in generate(irange(19, 27)):
          # generate the possible stacking order for the cards
          for (T, M, B) in itertools.permutations((C1, C2, C3)):
            # try the pin in each position
            for i in irange(0, 8):
              pin = (T[i], M[i], B[i])
              # are they all multiples of 3?
              if any(n for n in pin if n % 3 > 0): continue
              # and does it make a 5-digit square?
              n = int(concat(*pin))
              if not is_square(n): continue
              # find where the squares are on the top and bottom cards
              for j in irange(0, 8):
                if not(is_square(T[j]) and is_square(B[j])): continue
                printf("middle={m} [pin={pin} squares={s}]", m=M[j], s=(T[j], M[j], B[j]))
      # we only need to consider one orientation for one of the cards

    Solution: The number on the middle card is 7.

Leave a Comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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

%d bloggers like this: