title: Solving Singly Even Magic Square (Lux)
body: <p>Hey guys I am trying to implement a method for solving singly even magic square, but it seems to generating the wrong results. </p>
  
  <p><strong>Result generated by the following code</strong></p>
  
  <p><strong>Enter in size of square:</strong> 
  <strong>10</strong></p>
  
  <pre><code>75 75  0  0  0 50 50 50 50 25 
  75 75  0  0  0 50 50 50 50 25 
   0 75 75  0  0 50 50 50 50 25 
  75 75  0  0  0 50 50 50 50 25 
  75 75  0  0  0 50 50 50 50 25 
   0  0 75 75 75 25 25 25 25 50 
   0  0 75 75 75 25 25 25 25 50 
  75  0  0 75 75 25 25 25 25 50 
   0  0 75 75 75 25 25 25 25 50 
   0  0 75 75 75 25 25 25 25 50 
  </code></pre>
  
  <p><strong>The magic constant is 375</strong></p>
  
  <p><a href="http://en.wikipedia.org/wiki/LUX_method_for_magic_squares" rel="nofollow">Conway's LUX method for magic squares</a></p>
  
  <p><strong>Can anyone help me fix it or inform me on what is wrong or where my problems lie?</strong></p>
  
  <pre><code>import java.util.Scanner;
  
  public class MagicSquare {
  
  private static int[][] magicSquare; //initialization
  
  public static void main(String[] args) {
      Scanner input = new Scanner(System.in);
  
      int size = -1;
  
      System.out.println("Enter in size of square: ");
      size = input.nextInt();
  
      magicSquare = new int [size][size];
  
      singlyEvenMagicSquare(size);
      displaySquare();
  }
  
  public static void singlyEvenMagicSquare(int size)
  {
    int i, j, k, index=0;
  
    int p=size/2;
  
    int [][] M =  new int [p][p];
    //magicSquare = new int[size][size];
  
    for (i=0; i&lt;p; i++)
      for (j=0; j&lt;p; j++)
      {
        magicSquare[i][j]=M[i][j];
        magicSquare[i+p][j]=M[i][j]+3*p*p;
        magicSquare[i][j+p]=M[i][j]+2*p*p;
        magicSquare[i+p][j+p]=M[i][j]+p*p;
      }
  
    if (size==2)
      return;  
  
    int [] I = new int[p];
    int [] J = new int[size];
  
    for (i=0; i&lt;p; i++)
      I[i]=i+1;
  
    k=(size-2)/4;
  
    for (i=1; i&lt;=k; i++)
      J[index++] = i;
  
    for (i=size-k+2; i&lt;=size; i++)
      J[index++] = i;
  
    int temp;
    for (i=1; i&lt;=p; i++)
      for (j=1; j&lt;=index; j++)
      {
        temp=magicSquare[i-1][J[j-1]-1];
        magicSquare[i-1][J[j-1]-1]=magicSquare[i+p-1][J[j-1]-1];
        magicSquare[i+p-1][J[j-1]-1]=temp;
      }
  
    //j=1, i
    //i=k+1, k+1+p
    i=k; 
    j=0;
    temp=magicSquare[i][j]; 
    magicSquare[i][j]=magicSquare[i+p][j]; 
    magicSquare[i+p][j]=temp;
  
    j=i;
    temp=magicSquare[i+p][j]; 
    magicSquare[i+p][j]=magicSquare[i][j]; 
    magicSquare[i][j]=temp;
  
  
  }
  
  private static void displaySquare() {
      int magicConstant = 0;
      for (int j = 0; j &lt; magicSquare.length; j++) {
          for (int k = 0; k &lt; magicSquare[j].length; k++) {
              System.out.print(magicSquare[j][k] + " ");
          }
          System.out.print("\n");
          magicConstant = magicConstant + magicSquare[j][0];
      }
      System.out.print("The magic constant is " + magicConstant);
  }
  
  }
  </code></pre>
  
  <p><strong>Any help would be appreciated thanks!</strong></p>