From: munir
Subject: Magic square
Date: 
Message-ID: <38A2368B.65EE2067@rocketmail.com>
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

·········@rocketmail.com

From: F. Xavier Noria
Subject: Re: Magic square
Date: 
Message-ID: <nqq4askkg2uvfpm0nveug6boh7o00jeanf@4ax.com>
On Thu, 10 Feb 2000 04:04:58 GMT, munir <·········@rocketmail.com>
wrote:

: 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
From: Sandeep Koranne
Subject: Re: Magic square
Date: 
Message-ID: <yzwn1p8m5om.fsf@natlab.research.philips.com>
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
From: Christopher R. Barry
Subject: Just get the RREF. Re: Magic square
Date: 
Message-ID: <87bt5luddg.fsf_-_@2xtreme.net>
Sandeep Koranne <·······@natlab.research.philips.com> writes:

> 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
From: F. Xavier Noria
Subject: Re: Just get the RREF. Re: Magic square
Date: 
Message-ID: <h12dasg35mpu6c3rvcbulul9kemu36pmre@4ax.com>
On Sun, 13 Feb 2000 00:52:16 GMT, ······@2xtreme.net (Christopher R. Barry)
wrote:

:    +--                                   --+
:    | 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
From: Sandeep Koranne
Subject: Re: Just get the RREF. Re: Magic square
Date: 
Message-ID: <yzwn1p4mb50.fsf@natlab.research.philips.com>
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			 
E-mail: ···············@philips.com 
From: Rob Warnock
Subject: Re: Magic square
Date: 
Message-ID: <885clg$45j1l@fido.engr.sgi.com>
Sandeep Koranne  <·······@natlab.research.philips.com> wrote:
+---------------
| 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".

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