Results 1 to 15 of 15
  1. #1
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default 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. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    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

  4. #4
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    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.

  6. #6
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    I haven't quite figured out what you said yet, but I'm sure I'll figure it out. Thank you!

  7. #7
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    Ok, I think I get it now.

  8. #8
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

  9. #9
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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?

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    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

  11. #11
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

  12. #12
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    badadabump

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    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

  14. #14
    gandalf5166 is offline Member
    Join Date
    Feb 2010
    Posts
    75
    Rep Power
    0

    Default

    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.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by gandalf5166 View Post
    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

Similar Threads

  1. Calculating Squares and Cubes in a table
    By aldorfski_17 in forum New To Java
    Replies: 4
    Last Post: 03-22-2010, 07:17 PM
  2. Magic Square!!!... :D
    By joms999 in forum New To Java
    Replies: 4
    Last Post: 02-25-2010, 07:55 AM
  3. Magic Eightball
    By sachmow in forum New To Java
    Replies: 1
    Last Post: 11-15-2009, 04:37 PM
  4. help with perfect squares
    By AmplifiedKid in forum New To Java
    Replies: 1
    Last Post: 09-19-2009, 07:44 PM
  5. Perfect Squares
    By divyachaparala in forum New To Java
    Replies: 4
    Last Post: 02-05-2008, 09:21 AM

Posting Permissions

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