1

Hey guys I am trying to implement a method for solving singly even magic square, but it seems to generating the wrong results.

Result generated by the following code

Enter in size of square: 10

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 

The magic constant is 375

Conway's LUX method for magic squares

Can anyone help me fix it or inform me on what is wrong or where my problems lie?

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<p; i++)
    for (j=0; j<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<p; i++)
    I[i]=i+1;

  k=(size-2)/4;

  for (i=1; i<=k; i++)
    J[index++] = i;

  for (i=size-k+2; i<=size; i++)
    J[index++] = i;

  int temp;
  for (i=1; i<=p; i++)
    for (j=1; j<=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 < magicSquare.length; j++) {
        for (int k = 0; k < 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);
}

}

Any help would be appreciated thanks!

false
  • 10,264
  • 13
  • 101
  • 209
ferronrsmith
  • 1,110
  • 5
  • 28
  • 47
  • I don't believe the code you've posted will generate the output you've described. In particular, `magicSquare` is used before it is initialized -- unless the missing `magicSq` initializes it. Please post the actual code you used! – Gareth McCaughan Apr 06 '11 at 23:50
  • that was a error, i removed some code for clarity. corrected it. hope this helps – ferronrsmith Apr 07 '11 at 00:12
  • The current version gives a StackOverflowError. Can you please post a version that will produce the result from above? If you could also add some comments to indicate what parts of the algorithm each chunk of code is trying to accomplish, that'd be great. – lins314159 Apr 07 '11 at 00:40
  • @ferronrsmith: By convention, Java uses camelCase for methods/variables and PascalCase for class names. – Dave Jarvis Apr 07 '11 at 00:55
  • 1
    There is a very obvious bug, the matrix M[][] is read before it's initialized and you just read zeroes from it. – Antti Huima Apr 07 '11 at 04:46

1 Answers1

2
public class MagicSquare {

    private static int[][] magicSquare;
    private static int size; 

    public static void main(String[] args) {

        size = readValueFromFile();

        boolean validNum = false;

            if (size<3) {
                System.err.println("Enter a positive integer square size of 3 or more");
                validNum = false;
            } else {
                validNum = true;
            }

        if(validNum){
            magicSquare = new int [size][size];
            MagicSq(magicSquare,size);
            displaySquare();

        }
    }

    /**
     * The following function determines which magic square operation to perform given the order(n)
     * @param magicSquare
     * @param size
     */
    public static void MagicSq(int[][] magicSquare,int size) {
        if(size%2==1)
            OddMagicSquare(magicSquare,size);
        else if (size%4==0)
            DoubleEvenMagicSquare(magicSquare,size);
        else
            SinglyEvenMagicSquare(magicSquare,size);

    }

    public static void SinglyEvenMagicSquare(int[][] magicSquare,int size) {
      int i, j, k, index=0;

      int p=size/2;

      int [][] M =  new int [p][p];

      MagicSq(M,p);

      System.out.println();

      for (i=0; i<p; i++)
        for (j=0; j<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<p; i++)
        I[i]=i+1;

      k=(size-2)/4;

      for (i=1; i<=k; i++)
        J[index++] = i;

      for (i=size-k+2; i<=size; i++)
        J[index++] = i;

      int temp;
      for (i=1; i<=p; i++)
        for (j=1; j<=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;
        }

      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;
    }

   //truncated for clarity
}
ferronrsmith
  • 1,110
  • 5
  • 28
  • 47