# Thread: Magic squares LUX method

1. Member
Join Date
Feb 2010
Posts
75
Rep Power
0

## Magic squares LUX method

Hey, I'm trying to make a program that will make a magic square of any order. But I can't figure out how to implement the LUX method. I would love to give you what I have, but I really have no clue how to do it. Could you just give me some pseudocode? I can write the specifics, I am just completely lost on how to start. I have an array, and that's about it. Thank you SO much. I can't explain how the lux method works in text, so google it if you don't know what I'm talking about.
Edit: Here's a link:http://mathworld.wolfram.com/MagicSquare.html Scroll about halfway down the page. And yes, this is my homework, but I don't need an entire program. Just the pseudocode for one method.
Last edited by gandalf5166; 04-13-2010 at 04:37 AM.

2. Originally Posted by gandalf5166
Here's a link:Magic Square -- from Wolfram MathWorld Scroll about halfway down the page. And yes, this is my homework, but I don't need an entire program. Just the pseudocode for one method.
But the pseudo code (in plain English) is in that link. You basically create an odd size magic square but each square actually is made up of 2x2 squares. You have to fill each qraduple according to either the L, U or X shape. Carefully read the text in that link again.

kind regards,

Jos

3. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
Yeah, but I can't figure out how to do it with a computer. As far as I can tell, it's only really doable by a human being. Either that, or I don't know enough about Java to do it. And Eranga, here's the relevant paragraph, copy and pasted from the site.

A very elegant method for constructing magic squares of singly even order n=4m+2 with m>=1 (there is no magic square of order 2) is due to J. H. Conway, who calls it the "LUX" method. Create an array consisting of m+1 rows of Ls, 1 row of Us, and m-1 rows of Xs, all of length n/2=2m+1. Interchange the middle U with the L above it. Now generate the magic square of order 2m+1 using the Siamese method centered on the array of letters (starting in the center square of the top row), but fill each set of four squares surrounding a letter sequentially according to the order prescribed by the letter. That order is illustrated on the left side of the above figure, and the completed square is illustrated to the right. The "shapes" of the letters L, U, and X naturally suggest the filling order, hence the name of the algorithm.

4. Instead of marking those quadruples of squares with L, U or X mark them with 0, 1 and 2; have two 2x4 arrays ready: int[][] dx and int[][] dy as follows:

Java Code:
```int[][] dx= { { 1, -1, 1, -1 }, { 0, 0, 1, 0 }, { 0, 1, -1, 1 } }; // x direction for L, U or X
int[][] dy= { { 0, 1, 0, -1 }, { 0, 1, 0, -1 }, { 0, 1, 0, 01 } }; // y direction for L, U or X```
For any square at (x, y) and marked 0, 1 or 2 walk over the magic square for the x_i+1= x_i+dx[mark][i] and y_i+1= y_i+dy[mark][i] for the values i= 0, 1, 2 and 3 (all four squares in a quadruple. The value of 'mark' is 0, 1 or 2 given the current quadruple. You can figure out the rest.

kind regards,

Jos
Last edited by JosAH; 04-13-2010 at 05:38 PM.

5. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
I haven't quite figured out what you said yet, but I'm sure I'll figure it out. Thank you!

6. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
Ok, I think I get it now.

7. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
Ok, I don't really get it. I've only just been introduced to arrays. What you just said doesn't really make sense. Could you explain it to me in English, and maybe show me an example? And please, just an example, I really do want to learn how to do it.

8. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
No, I get it now. Those are two-dimensional arrays showing the change in each coordinate. So the {}'s declare each set of four as 0, 1, and 2? But then how would I move up and to the right a whole set of four? I wouldn't know where I was, since the different letters end in different places. Also, would I create a separate array that had the different "letters" in it? Or could that somehow be done within the square? And if it would have to be in a different array, how would I access it in such a way that it would correspond to the real one, which would be four times bigger?

9. Originally Posted by gandalf5166
No, I get it now. Those are two-dimensional arrays showing the change in each coordinate. So the {}'s declare each set of four as 0, 1, and 2? But then how would I move up and to the right a whole set of four? I wouldn't know where I was, since the different letters end in different places. Also, would I create a separate array that had the different "letters" in it? Or could that somehow be done within the square? And if it would have to be in a different array, how would I access it in such a way that it would correspond to the real one, which would be four times bigger?
You always start at the upper left corner of each 2x2 square by using two simple loops:

Java Code:
```for (int y= 0; y < n; y+=2) {
for((int x= 0; x < n; x+=2) {
int yy= y; // top of little 2x2 square
int xx= x; // left of little 2x2 square
// ...
}
}```
In the body of the inner loop you use the dy and dx arrays to traverse those 2x2 little squares. But first you check the value of the big square magic[yy][xx] which dx and dy arrays to use (a 0, 1 or 2 value for an L, U, X move). Of course you overwite that magic[yy][xx] value but you don't care because after you have filled in the correct values you never consult it again and you don't care 'where you end' because you fill in the next 2x2 square the next time you go through the loops.

kind regards,

Jos

10. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
Two questions. What is n? And I can't tell, are you saying that I do need two arrays? Edit: I get it,I'm making an array with the "letters" and an array that is the square, calculating that since I'm at the upper left of the little square then I can know where I am in relation to the "letters" array. Still don't know what n is though.
Last edited by gandalf5166; 04-16-2010 at 09:10 PM.

11. Member
Join Date
Feb 2010
Posts
75
Rep Power
0

12. Originally Posted by gandalf5166
Two questions. What is n? And I can't tell, are you saying that I do need two arrays? Edit: I get it,I'm making an array with the "letters" and an array that is the square, calculating that since I'm at the upper left of the little square then I can know where I am in relation to the "letters" array. Still don't know what n is though.
'n' is the size of a side of your magic square; first you have to fill it with the values 0, 1 and 2 (for the values L, U and X); the next pass fills it with the values for the final magic square as I described before. The dx and dy arrays indeed describe the geometric shapes for L, U and X for each 2x2 square. Those are all the arrays you need: one big magic square array and two small direction arrays (those are the dx and dy arrays)

kind regards,

Jos

13. Member
Join Date
Feb 2010
Posts
75
Rep Power
0
Thank you so much! This is due tonight and there is almost no way I would be able to have it done by then without your help.

14. Originally Posted by gandalf5166
Thank you so much! This is due tonight and there is almost no way I would be able to have it done by then without your help.
Don't forget to 'walk' over that magic square nxn as if it were a magic sqare of odd size (n/2)x(/2). Maybe if you google for "magic square Horsmeier" you can find a solution of mine that enumerates the values for an odd size square from left to right, top to bottom.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•