Skip to main content

list manipulation - Solving the magic backyard puzzle with MMA


I was trying to solve this puzzle using mma



Mathematica graphics


It looks like the generalization of the magic square. Anyway here my attempt:-


There are four columns $(x,y,z,w)$ and nine rowa which are denoted by the array index.


First i define the variables which have zero values


x[1] = 0; x[2] = 0; x[3] = 0; x[9] = 0; w[1] = 0;

Then the equation equating all the columns


c[1] = Sum[x[i], {i, 4, 8}];
c[2] = Sum[y[i], {i, 1, 9}];
c[3] = Sum[z[i], {i, 1, 9}];

c[4] = Sum[w[i], {i, 2, 9}];

col = Equal @@ Thread[Array[c, 4]]

Next for the rows


r[n_] := x[n] + y[n] + z[n] + w[n];
row = Equal @@ Thread[Array[r, 9]]

Here we can give some upper bound for the solutions.


all = Join[Array[x, 5, {4, 8}], Array[y, 9], Array[z, 9], Array[w, 9]];

And @@ Thread[0 <= all <= 100]

For distinct solutions:


allne = Unequal @@ 
Thread[Join[Array[x, 5, {4, 8}], Array[y, 9], Array[z, 9],
Array[w, 8, {2, 9}]]]

List of all variables


allvar = Join[
Join[Array[x, 5, {4, 8}], Array[y, 9], Array[z, 9],

Array[w, 8, {2, 9}]]]

Finally when I use FindInstance it just keeps running(its been 2hr now).


FindInstance[{col && row && And @@ Thread[0 <= all <= 100] && 
allne}, allvar, Integers]

So my question is why is FindInstance so slow even after putting bounds on integer solutions.Also can this be done by any other method.


I also tried the simplest $3 \times 3$ magic square


FindInstance[
a + b + c == d + e + f == g + h + i == a + d + g == b + e + h ==

c + f + i && a != b != c != d != e != f != g != h != i, {a, b, c,
d, e, f, g, h, i}, Integers]

This also kept on running. Do you think that i just need to wait for the output because for these kind of sums mma will take that long?




Comments