Magic Square : Java : BlueJ

Objective :

Magicsquareexample.svgThe BlueJ Based Program which Prints the Magic Square Pattern is based on Matrix of Odd Order. In recreational mathematics, a magic square of order n is an arrangement of n2 numbers, usually distinct integers, in a square, such that the n numbers in all rows, all columns, and both diagonals sum to the same constant. A normal magic square contains the integers from 1 to n2. The term "magic square" is also sometimes used to refer to any of various types of word square.
Normal magic squares exist for all orders n ≥ 1 except n = 2, although the case n = 1 is trivial, consisting of a single cell containing the number 1. The smallest nontrivial case, shown below, is of order 3.
The constant sum in every row, column and diagonal is called the magic constant or magic sum, M. The magic constant of a normal magic square depends only on n and has the value
M = \frac{n(n^2+1)}{2}.
For normal magic squares of order n = 3, 4, 5, ..., the magic constants are:
15, 34, 65, 111, 175, 260, ... (sequence A006003 in OEIS).

The Program has Basically 4 Algorithms to compute and Generate The Magic Square Pattern :

Algorithm 1 :
                      The Code starts Executing from the Center of the Top Row of the Square matrix. The Pointer here always moves in Top-Left Direction after the Element is inserted. If the Pointer falls off the top of Array index, The Pointer is moved to the bottom of the current column. If the Pointer falls of the Left side, the Pointer is moved to the Last column of the current Row. If It falls from the Corner (Top-Left) or the Element is Already present, the Pointer Climbs down 2 steps and Moves right 1 step and continues the Process till All the Positions are Filled.

Algorithm 2 :
                      The Code starts Executing from the Centre of the Top Row of the Square matrix. The Pointer here always moves in Top-Right Direction after the Element is inserted. If the Pointer falls off the top of Array index, The Pointer is moved to the bottom of the current column. If the Pointer falls of the Right side, the Pointer is moved to the First column of the current Row. If It falls from the Corner (Top-Right) or the Element is Already present, the Pointer Climbs down 2 steps and Moves left 1 step and continues the Process till All the Positions are Filled.

Algorithm 3 :
                      The Code starts Executing from the Centre of the Bottom Row of the Square matrix. The Pointer here always moves in Bottom-Left Direction after the Element is inserted. If the Pointer falls off the bottom of Array index, The Pointer is moved to the top of the current column. If the Pointer falls of the Left side, the Pointer is moved to the Last column of the current Row. If It falls from the Corner (Bottom-Left) or the Element is Already present, the Pointer Climbs up 2 steps and Moves right 1 step and continues the Process till All the Positions are Filled.

Algorithm 4 :
                      The Code starts Executing from the Centre of the Bottom Row of the Square matrix. The Pointer here always moves in Bottom-Right Direction after the Element is inserted. If the Pointer falls off the bottom of Array index, The Pointer is moved to the top of the current column. If the Pointer falls of the Right side, the Pointer is moved to the First column of the current Row. If It falls from the Corner (Bottom-Right) or the Element is Already present, the Pointer Climbs up 2 steps and Moves left 1 step and continues the Process till All the Positions are Filled.
Yanghui Magic Square

Algorithm 1 :      Algorithm 2 :        Algorithm 3 :      Algorithm 4 :
15  8  1 24 17     17 24  1  8 15       9  2 25 18 11     11 18 25  2  9
16 14  7  5 23     23  5  7 14 16       3 21 19 12 10     10 12 19 21  3
22 20 13  6  4      4  6 13 20 22      22 20 13  6  4      4  6 13 20 22
 3 21 19 12 10     10 12 19 21  3      16 14  7  5 23     23  5  7 14 16
 9  2 25 18 11     11 18 25  2  9      15  8  1 24 17     17 24  1  8 15

BlueJ Program Screenshot :



Java Program Source Code :

/**
 *  Algorithm 1 :      Algorithm 2 :        Algorithm 3 :      Algorithm 4 :
 *  
 *  15  8  1 24 17     17 24  1  8 15       9  2 25 18 11     11 18 25  2  9
 *  16 14  7  5 23     23  5  7 14 16       3 21 19 12 10     10 12 19 21  3
 *  22 20 13  6  4      4  6 13 20 22      22 20 13  6  4      4  6 13 20 22
 *   3 21 19 12 10     10 12 19 21  3      16 14  7  5 23     23  5  7 14 16
 *   9  2 25 18 11     11 18 25  2  9      15  8  1 24 17     17 24  1  8 15
 *   
 * @author SHANTANU KHAN
 * @mail shantanukhan1995@gmail.com
 * @website 0code.blogspot.com
 * Program Type : BlueJ Program - Java
 */
import java.util.Scanner;
public class MagicSquare
{
    private int[][] M;
    private static int order;
    public MagicSquare(int order)
    {
        this.order=order;
        M=new int[order][order];
    }
    public void magicFill1()
    {
        if(order%2==0)
        {   System.out.println("\nERROR : ORDER MUST BE ODD");    return; }
        int r=0,c=order/2,n=1;
        while(n<=order*order)
        {
            M[r][c]=n++;
            r--; c--; // MOVES TOP LEFT CORNER
            if(r<0&&c>=0) // FALLING OFF THE TOP
            {   r=order-1;  }
            else if(c<0&&r>=0) // FALLING FROM LEFT SIDE
            {   c=order-1;  }
            else if(c<0&&r<0||M[r][c]>0) // FALLING FROM CORNER OR ELEMENT ALREADY FILLED
            {   r+=2;   c++; } // DOWN TWO AND RIGHT ONE
        }
        magicPrint();
    }
    public void magicFill2()
    {
        if(order%2==0)
        {   System.out.println("\nERROR : ORDER MUST BE ODD");    return; }
        int r=0,c=order/2,n=1;
        while(n<=order*order)
        {
            M[r][c]=n++;
            r--; c++; // MOVES TOP RIGHT CORNER
            if(r<0&&c<=order-1) // FALLING OFF THE TOP
            {   r=order-1;  }
            else if(c>order-1&&r>=0) // FALLING FROM RIGHT SIDE
            {   c=0;  }
            else if(c>order-1&&r<0||M[r][c]>0) // FALLING FROM CORNER OR ELEMENT ALREADY FILLED
            {   r+=2;   c--; } // DOWN TWO AND LEFT ONE
        }
        magicPrint();
    }
    public void magicFill3()
    {
        if(order%2==0)
        {   System.out.println("\nERROR : ORDER MUST BE ODD");    return; }
        int r=order-1,c=order/2,n=1;
        while(n<=order*order)
        {
            M[r][c]=n++;
            r++; c--; // MOVES BOTTOM LEFT CORNER
            if(r>order-1&&c>=0) // FALLING OFF THE BOTTOM
            {   r=0;  }
            else if(c<0&&r<=order-1) // FALLING FROM LEFT SIDE
            {   c=order-1;  }
            else if(c<0&&r>=order||M[r][c]>0) // FALLING FROM CORNER OR ELEMENT ALREADY FILLED
            {   r-=2;   c++; } // TOP TWO AND RIGHT ONE
        }
        magicPrint();
    }
    public void magicFill4()
    {
        if(order%2==0)
        {   System.out.println("\nERROR : ORDER MUST BE ODD");    return; }
        int r=order-1,c=order/2,n=1;
        while(n<=order*order)
        {
            M[r][c]=n++;
            r++; c++; // MOVES BOTTOM RIGHT CORNER
            if(r>order-1&&c<=order-1) // FALLING OFF THE BOTTOM
            {   r=0;  }
            else if(c>order-1&&r<=order-1) // FALLING FROM RIGHT SIDE
            {   c=0;  }
            else if(c>order-1&&r>order-1||M[r][c]>0) // FALLING FROM CORNER OR ELEMENT ALREADY FILLED
            {   r-=2;   c--; } // TOP TWO AND RIGHT ONE
        }
        magicPrint();
    }
    public void magicPrint()
    {
        System.out.print(" Magic Square Generated :\n\n");
        for(int i=0;i<order;i++)
        {   for(int j=0;j<order;j++)
            {   print(M[i][j],order*order);  }
            System.out.println();
        }
    }
    private void print(int num,int max) // SPECIAL PRINT WHICH CALCULATES THE SPACES
    {
        String n=new String()+num; String m=new String()+max;
        for(int i=0;i<=(m.length()-n.length());i++) System.out.print(" ");
        System.out.print(n);
    }
    private void clear(){for(int i=0;i<order;i++){for(int j=0;j<order;j++){M[i][j]=0;}}}
    public static void main(String args[])throws Exception
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter the Order : "); order=sc.nextInt();
        if(!(order%2==0)){
        MagicSquare obj=new MagicSquare(order);
        System.out.print("\nAlgorithm 1 : "); obj.magicFill1(); obj.clear();
        System.out.print("\nAlgorithm 2 : "); obj.magicFill2(); obj.clear();
        System.out.print("\nAlgorithm 3 : "); obj.magicFill3(); obj.clear();
        System.out.print("\nAlgorithm 4 : "); obj.magicFill4(); obj.clear();}
        else System.out.println("The Order must be Odd");
    }
}


    


© Shantanu Khan 0code ® Magic Square : BlueJ