Thanks Thomas, works very well!
> From: [email protected]
> Date: Tue, 22 Dec 2015 12:14:31 -0500
> To: [email protected]
> Subject: Re: [Jprogramming] Magic squares
>
> I haven't tried this in a while but here is one method from an APL book
> transcribed into J
>
> NB. Magic Squares the APL/J way
> NB. From APL An Interactive Approach by Gilman and Rose
> NB.
> NB. Problem 19 page 177
> NB.
> NB. A magic square of order n made up of the integers from
> NB. 1 through n.
> NB.
> NB. In creating squares of odd order you can use a rotation
> NB. vector constructed from n successive integers with 0 in
> NB. the middle then using the vector to control the rotation
> NB. of the rows and columns of a matrix created with
> NB. successive integers (in J i. n n)
>
> NB. rotr - this uses the rotate operator but sets it to rotate
> NB. an individual row. Otherwise it will try to rotate
> NB. the rows in the matrix as a whole interchanging full
> NB. rows rather than shifting the element in a row and
> NB. leaving the rows in place
> rotr =: |."0 1
>
> NB. rotc - rotate the elements in their respective columns.
> NB. there is no way to tell J to specifically operate on
> NB. the columns individually so you need to use the
> NB. transpose op. (|:) to turn the columns into rows by
> NB. using the &. operator it will transpose, run the rotate
> NB. then transpose back
> rotc =: |."0 1&.|:
>
>
> NB. MS - magic square routine no bounds check just input an
> NB. odd number greater than or equal to 3
>
> MS =: 3 : 0
> NB. Create an initial square matrix order n of numbers 1 to n
> z =. 1 + i. y,y
>
> NB. Creat a rotation vector from -n/2 ... 0 ... n/2
> q =. ( - (<. 0.5 * y))+ i.y
>
> NB. rotate the rows of the matrix my q then rotate the columns
> NB. of the answer by q. You will get a new matrix that is a
> NB. magic square
> z1 =. q rotc q rotr z
> )
>
> Tom McGuire
>
> On Dec 22, 2015, at 9:44 AM, Richard Donovan <[email protected]> wrote:
>
> > Is there a J routine to construct magic squares of side n?
> >
> > Thanks in advance.
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm