Enigmatic Code

Programming Enigma Puzzles

Enigma 1641: 3 × 3 = 1

From New Scientist #2807, 9th April 2011 [link]

Joe printed and cut out nine 3 by 3 cardboard magic squares. The nine squares used all the numbers from 1 to 81.

For a square to be magic, the sums of the numbers in each row, each column and each diagonal must all be the same. This sum is called the magic constant.

Joe gave the nine magic squares to Penny and suggested she arrange them to make a 9 by 9 magic square and tell him the magic constant of the 3 by 3 square she placed in the middle.

What was the magic constant?

[enigma1641]

Advertisements

One response to “Enigma 1641: 3 × 3 = 1

  1. jimrandell 7 December 2011 at 10:23 pm

    The following Python code runs in 200ms.

    # 9x9 square sum is 369
    # so: a+b+c = d+e+f = g+h+i = a+d+g = b+e+h = c+f+i = 369
    # constants could be:
    # 15, 42, 69, 96, 123, 150, 177, 204, 231
    
    from itertools import permutations
    
    c = 369
    for s in permutations(range(15, 232, 27), 9):
      if sum(s[0:3]) != c: continue
      if sum(s[3:6]) != c: continue
      # if sum(s[6:9]) != c: continue
      if s[0] + s[3] + s[6] != c: continue
      if s[1] + s[4] + s[7] != c: continue
      # if s[2] + s[5] + s[8] != c: continue
      if s[0] + s[4] + s[8] != c: continue
      if s[2] + s[4] + s[6] != c: continue
      print(s[4], s)
    

    Solution: The magic constant on the central 3×3 square is 123.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: