Magic square
Author Message Magic square

Any one know wher can I find a lisp code for generating Magic squares
3X3 or 4X4
or any using search methods or or others

Thanks for the help

Mon, 29 Jul 2002 03:00:00 GMT  Magic square

: Any one know wher can I find a lisp code for generating Magic squares
: 3X3 or 4X4

Well, this sounds like homework, isn't it?

Sometime ago I wrote a program to search all the magic squares of a
given order, so I can provide some hints:

1. As you surely know, the magic sum is a function of the order.
2. Remember that different cells must hold different numbers.
3. Take into account that when a row/col/diag is filled but one
cell, then that cell can hold a unique value because the sum
of the row/col/diag must be the magic sum.
4. The fewer [sort of] loops, the better.

Regards,

-- Xavier

Mon, 29 Jul 2002 03:00:00 GMT  Magic square
Magic Sqares of 3x3 and 4x4 have a lot of differences.

Odd Magic squares can be easily computed using GO-UP GO-LEFT AND ADD method.

Even magic squares a a bit more tricky.

As previously pointed out : it looks like an assignment...but
the odd square method is given in many DS books.

HTH
sandeep

Tue, 30 Jul 2002 03:00:00 GMT  Magic square

Quote:

> Magic Sqares of 3x3 and 4x4 have a lot of differences.

> Odd Magic squares can be easily computed using GO-UP GO-LEFT AND ADD method.

> Even magic squares a a bit more tricky.

> As previously pointed out : it looks like an assignment...but
> the odd square method is given in many DS books.

Could you explain the method in more detail? I'd think one of the
better ways to do them would be to recognize a magic square as:

+--           --+
| x_1  x_2  x_3 |
|               |
| x_4  x_5  x_6 |
|               |
| x_7  x_8  x_9 |
+--           --+

You'd then have 8 equations in 9 unknowns like:

x_1 + x_2 + x_3  =  (x_7 + x_8 + x_9)
x_4 + x_5 + x_6  =  (x_7 + x_8 + x_9)
x_7 + x_8 + x_9  =  (x_7 + x_8 + x_9)
x_1 + x_4 + x_7  =  (x_7 + x_8 + x_9)
...
x_3 + x_5 + x_7  =  (x_7 + x_8 + x_9)

The augmented matrix of the system would then be:

+--                                   --+
| 1 1 1 0 0 0 0 0 0   (x_7 + x_8 + x_9) |
| 0 0 0 1 1 1 0 0 0   (x_7 + x_8 + x_9) |
| 0 0 0 0 0 0 1 1 1   (x_7 + x_8 + x_9) |
| 1 0 0 1 0 0 1 0 0   (x_7 + x_8 + x_9) |
| 0 1 0 0 1 0 0 1 0   (x_7 + x_8 + x_9) |
| 0 0 1 0 0 1 0 0 1   (x_7 + x_8 + x_9) |
| 1 0 0 0 1 0 0 0 1   (x_7 + x_8 + x_9) |
| 0 0 1 0 1 0 1 0 0   (x_7 + x_8 + x_9) |
+--                                   --+

Obtain the reduced row-echelon-form, and then you can just plug in
anything into the parameters that are left over and get a magic
square.

Christopher

Thu, 01 Aug 2002 03:00:00 GMT  Magic square

+---------------
| Magic Sqares of 3x3 and 4x4 have a lot of differences.
| Odd Magic squares can be easily computed using GO-UP GO-LEFT AND ADD method.
+---------------

Yup. Or as I learned it, UP+RIGHT -- OOPS,STEP-DOWN.

+---------------
| Even magic squares a a bit more tricky.
+---------------

I vaguely recall some procedure for even magic squares in which you
first factor the square into even and non-even components, so you have
an even supersquare of odd subblocks. Then you:

1. Solve the even supersquare by some method [e.g., see below]

2. Solve one off subblock

3. Put the solved odd subblock wherever the "1" is in the even
superblock solution, and fill in the remaining subblocks with
multiples of odd solution, using the number in the even superblock
position as a multiplier for every element of the subblock.

Or something like that...

-Rob

p.s. Aha! The Web is your friend!

<URL:http://mathworld.wolfram.com/MagicSquare.html> gives a method for
"doubly-even" squares that are 4*m on a side ("draw X's through the four
subsquares...then swap..."), and one for "singly-even" squares 4*m+2 on
a side [for m>1 only!] (which J. H. Conway calls the "LUX" method).

That page also calls the "UP+RIGHT/STEP-DOWN" method for odd squares
"the Siamese method".

-----

Applied Networking              http://reality.sgi.com/rpw3/
Silicon Graphics, Inc.          Phone: 650-933-1673
1600 Amphitheatre Pkwy.         PP-ASEL-IA
Mountain View, CA  94043

Thu, 01 Aug 2002 03:00:00 GMT  Magic square

:    +--                                   --+
:    | 1 1 1 0 0 0 0 0 0   (x_7 + x_8 + x_9) |
:    | 0 0 0 1 1 1 0 0 0   (x_7 + x_8 + x_9) |
:    | 0 0 0 0 0 0 1 1 1   (x_7 + x_8 + x_9) |
:    | 1 0 0 1 0 0 1 0 0   (x_7 + x_8 + x_9) |
:    | 0 1 0 0 1 0 0 1 0   (x_7 + x_8 + x_9) |
:    | 0 0 1 0 0 1 0 0 1   (x_7 + x_8 + x_9) |
:    | 1 0 0 0 1 0 0 0 1   (x_7 + x_8 + x_9) |
:    | 0 0 1 0 1 0 1 0 0   (x_7 + x_8 + x_9) |
:    +--                                   --+
:
: Obtain the reduced row-echelon-form, and then you can just plug in
: anything into the parameters that are left over and get a magic
: square.

I programmed my finder codifying the cells to emulate nested loops.

0 = loop
1 = it is the last in its row
2 = it is the last in its col
4 = it is the last in diag1
8 = it is the last in diag2

and then writing a path looking at every stage for the shortest way
to the sides/corners, and a matrix with the codes. For order 5:

Path
----
1  2  3  4  5
6 10 11 12 13
7 16 14 18 19
8 15 20 22 23
9 17 21 24 25

Codes
-----
0 0 0 0 1
0 0 0 0 1
0 0 0 0 1
0 8 0 0 1
2 2 2 2 7

This essentially solves the system, but the way to avoid innecessary
permutations of possible values of the parameters is embedded in the
path and the codes, and it is very easy to avoid isomorphic squares
because you can take a representant (of classes of 8 squares) looking
at the values of the corners.

The routine is short, efficient (well, less than 2 seconds to find
the 880 of order 4 in a Celeron 300) and is practically a description
in English of the search, `a la' CWEB.

-- Xavier

Thu, 01 Aug 2002 03:00:00 GMT  Magic square
Hello.
Maybe I have got your question ALL wrong : but here is
what I know of ODD magic squares :

- 1 -
- - -
- - -

- 1 -
- - -
2 - -

- 1 -
- - 3
2 - -

- 1 -
- - 3
2 - 4

- 1 -
- 5 3
2 - 4

6 1 -
- 5 3
2 - 4

6 1 -
7 5 3
2 - 4

6 1 8
7 5 3
2 - 4

6 1 8
7 5 3
2 9 4

You start by putting 1 in the top row middle.
then inceremt -> go up -> go left.
If position is filled put new number is bottom of
old position.

It also works if you go right and bottom.

HTH
Sandeep.

"Lets Make Things Better"
ED&T Test, WAY 3.37
Philips Research Laboratories                   Botstraat, 7
5656 AA, Eindhoven                              5654  NL, Eindhoven
The Netherlands                                 The Netherlands
Phone: +31-(040)-27 45250                       +31-(040)-2573492
Fax  : +31-(040)-27 44626

Fri, 02 Aug 2002 03:00:00 GMT

 Page 1 of 1 [ 10 post ]

Relevant Pages