Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: MAGIC SQUARE: Python & JAVA CODE

Friday, August 8, 2014

MAGIC SQUARE: Python & JAVA CODE

In recreational mathematics, a magic square is an arrangement of distinct numbers (i.e. each number is used once), usually integers, in a square grid, where the numbers in each row, and in each column, and the numbers in the forward and backward main diagonals, all add up to the same number. A magic square has the same number of rows as it has columns, and in conventional math notation, "n" stands for the number of rows (and columns) it has. Thus, a magic square always contains n2 numbers, and its size (the number of rows [and columns] it has) is described as being "of order n". A magic square that contains the integers from 1 to n2 is called a normal magic square.

La Loubere's method ( for odd order)
The integers from 1 to n2 are inserted along diagonals, starting in the middle of first row and heading in a northeasterly direction. When you go off an edge of the array, which you do at the very first step, continue from the opposite edge. When you bump into a cell that is already occupied, drop down one row and continue.



Python Code

#Magic Square
def MagicSquare():
    N = input("Enter the order of the Magic Square (odd):")
    MS=[[ 0 for row in range(N+1)]for col in range(N+1)]
    row = 1
    col = (N+1)/2
    for i in range(N*N):
         MS[row][col] =i+1
         row -=1
         col +=1
         trow = row
         tcol = col
         if row<=0:
            row = N
         if col>N:
            col = 1
         if MS[row][col] != 0:
            if row == N:
                row = 0           
            if col == 1:
                col = N+1  
            row +=1
            col -=1
            row +=1  
         if row<=0:
            row = N
         if col>N:
            col = 1
    print "OUTPUT~~ MAGIC SQUARE:"
    for i in range (N):
        for j in range (N):
            print MS[i+1][j+1],'\t',
        print
MagicSquare()         




JAVA Code

import java.util.*;
public class MagicSquare {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[][] MS;
        int row,col,N;
        int trow,tcol;   
        System.out.println("Enter the order of the matrix:(odd)");
        N = in.nextInt();
        MS = new int[N+1][N+1];
        row = 1;
        col = (N+1)/2;
        for(int i = 0; i<N+1;i++)
            for(int j = 0; j<N+1;j++)
                MS[i][j] =0;
        for (int k =1 ; k <=N*N; k++) {
        MS[row][col] =k;
        row--;
        col++;
        trow = row;
        tcol = col;
        if(row<=0)
            row = N;
        if(col>N)
            col = 1;   
        if(MS[row][col] != 0) {
            if ( row == N )
                row = 0;            
            if ( col == 1)
                col = N+1;   
            row++;
            col--;
            row++;
            }   
        if(row<=0)
            row = N;
        if(col>N)
            col = 1;   
        }
        System.out.println("OUTPUT~~ MAGIC SQUARE:");
        for(int i = 1; i<=N;i++) {
            for(int j = 1; j<=N;j++) {
            String t =" ";
                if(MS[i][j]<10) {
                    t = t+" ";
                    t +=MS[i][j];
                    System.out.print("   "+t);
                    }
                else if (10 <= MS[i][j] && MS[i][j] <=99) {
                    t +=MS[i][j];
                    System.out.print("   "+t);
                    }   
                else   
                System.out.print("   "+MS[i][j]);   
                }
            System.out.println();
            }   
        }
    }   

No comments:

Post a Comment