How to Build a Magic Square of Odd Order

Building a magic square of odd order is easier than building one of even order. This is because singly even (4n+2) order and doubly even(4n) order magic squares work differently. To build a magic square of odd order, follow the following steps.
  • Set an initial value of zero to all the elements of the matrix.
  • Set the smallest number in the magic square. 1 is an obvious choice. But, again a choosing a higher number (x) is easy. This element will appear in the middle of the first row. Consequently, the highest number will be (x+ ord*ord-1), where ord is the order of the matrix and '*' is the multiplication sign. The order will be read at the time of execution along with the first element.
  • Now, the next element is to be set such that it is in the previous row #row-1and the next column #col+1 if the slot is empty. Sometimes, when you are in row #1 or column #3, the previous row becomes row #0 and the next column becomes col #(ord+1). In this case, you must reset the row to row #ord and column to col #1. Also, if the slot is not empty in any case, then the element is filled in the slot directly below i.e. row #(row+1), col #col, which is the same column.
 Let's watch a video demonstrating a 5x5 magic square.


Now for the FORTRAN program,

!To make a magic square of an odd order
program magicsqodd
    implicit none
    integer i,j,ord,k,mat(100,100),n,row,col,nxtrow,nxtcol,matel,x,sum1,sum2,sum3,sum4 !Sum1 to sum4 are optional
    write(*,*) "Assign the order of the magic square (odd number<100)"
    read (*,*) ord
    write (*,*) "Magic square for matrix of order=",ord
   
    write (*,*) "Enter the first number of the magic square"
    read (*,*) x
    write (*,*) "Magic Square starting with the number", x
   
    !To set elements equal to zero initially
    do i=1,ord
        do j=1,ord
            mat (i,j)=0
        end do
    end do
   
    !To set the value for the first element
    row=1
    col=ord/2 +1
    mat(row,col)=x
   
    n=(x-1)+ord*ord !The highest number in the magic square
   

    do k=(x+1),n
        !For the next element
        nxtrow=row-1
        nxtcol=col+1
        !To reset on going out of the matrix
        if (nxtrow.lt.1) then
            nxtrow=ord
        end if
        if (nxtcol.gt.ord) then
            nxtcol=1
        end if
        matel=mat(nxtrow,nxtcol) !To check whether the next place is filled
        if (matel/=0) then
            nxtrow=row+1
            nxtcol=col
        end if
        row=nxtrow !To keep on changing row and column number permanently as the matrix progresses
        col=nxtcol
           mat(row,col)=k
    end do

    do i=1,ord !To display the matrix
          write (*,*) "The magic square is displayed below"
        write(*,*) (mat(i,j),j=1,ord)
    end do

!To check sums of rows, columns and diagonals. This part of the program is optional
    do i=1,ord !To check sums of elements in rows
        sum1=0
        do j=1, ord
            sum1=mat(i,j)+sum1
        end do
        write (*,*) "sum for row", i,"is", sum1
    end do

    do j=1,ord !To check sums of elements in columns
        sum2=0
        do i=1, ord
            sum2=mat(i,j)+sum2
        end do
        write (*,*) "sum for column", j,"is", sum2
    end do

    sum3=0
    do i=1,ord !To check sums of elements along both diagonals
        j=i
        sum3=mat(i,j)+sum3
    end do
    write (*,*) "sum for diagonal 1 is", sum3

    sum4=0
    do i=1,ord !To check sums of elements along both diagonals
        j=(ord+1-i)
        sum4=mat(i,j)+sum4
    end do
    write (*,*) "sum for diagonal 2 is", sum4
      
end program magicsqodd

Sample Output:
Output for order 7 Magic Square beginning with 89

No comments:

Post a Comment