Magic squares

Problem

Derive a 3×3 grid with random arrangements of number from 1 to 9 inclusive, where the sum of the diagonal, vertical and horizontal numbers are equal.
A | B | C
D | E | F
G | H | I
A+B+C = D+E+F = G+H+I = A+E+I = C+E+G

Most people may not know but this is actually an IQ test problem and falls under what we call an optimization problem. Optimization problems can be solved by brute force or more efficiently using genetic algorithms. Since the search space is quite small (84 combinations) I solved this quickly using a brute force method

First set the stage by importing random, setting variables and sample space

import random
k = [x for x in xrange(1,10)]
k1,k2,k3 = [1],[2],[3]

Then keep looking until the diagonals, horizontals and verticals are the same sum

while sum(k1)!=sum(k2)\
or sum(k2)!=sum(k3)\
or sum([k1[0],k2[1],k3[2]]) == sum(k1)\
or sum([k1[2],k2[1],k3[0]]) == sum(k2):
    k1 = random.sample(k,3)
    k2 = [x for x in random.sample(k,9) if x not in k1][:3]
    k3 = [x for x in random.sample(k,9) if x not in k1 and x not in k2][:3]

print k1, sum(k1)
print k2, sum(k2)
print k3, sum(k3)
print sum(k1),sum(k2),sum(k3)

This same problem can be solved in R using the magic package

#install.packages("magic")
library(magic)
magic(3)

And manually by arranging the numbers in order 1 2 3 4 5 6 7 8 9. We see that 5 is a centriod with equal amount of numbers on either end, but their sums are imbalanced (sum(1,2,3,4) <> sum(6,7,8,9)). So first re-arrange the numbers so that their sums are equal 8 2 3 7 5 4 6 1 9 (sum(8,2,3,7) = sum(4,6,1,9)). Then take contiguous numbers from either side starting from outer and sum it with 5, eg. 951 = 852 = 357 = 456, notice 5 is always in the center. Five now becomes your center box and the other numbers make a cross around 5.
? 7 ?
9 5 1
? 3 ?
Now you just need to fit in the 456 and 852. I will leave you to comment, with your answer. Gotta love math!

4 thoughts on “Magic squares

  • February 19, 2016 at 4:44 pm
    Permalink

    2 | 7 | 6
    9 | 5 | 1
    4 | 3 | 8

    Reply
  • February 19, 2016 at 5:24 pm
    Permalink

    Except I don’t love math. This was beautifully written though haha!

    Reply
  • April 27, 2016 at 9:03 pm
    Permalink

    Kimanii, that’s so funny, I was just thinking about magic squares yesterday. I had to program that for one of my C++ assignments years ago. I did a nice job and even had it draw boxes around each of the squares (that wasn’t part of the assignment and no one else did it but I got bonus points).

    So, recently I was thinking about using that as an assigned to start teaching my 8 & 9 year old kids VB.net and found some sample code on the web.

    I’ll have to check out your code.

    Thanks for posting!

    Reply
    • May 18, 2016 at 6:17 am
      Permalink

      For kids I would go with Python. I’m actually planning on teaching my son, programming over the summer using python and maybe throwing in some raspberry pi projects.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.