How to Build a 4x4 Magic Square

A 4x4 Magic Square is the first magic square of even order as a 2x2 magic square is not possible. This magic square can be made simply by flipping both the diagonals.
  • First off, we need to assign  the order 4 to the magic square and set a number as the value of the smallest element of the magic square. Of course, we can always begin with 1, but  it's not all that difficult to set another value.
  • Next, we need assign values to each square of the matrix, starting from the initial value (x) specified  to x+ (ord*ord -1), where '*' refers to multiplication and ord is the order of the matrix.This is because, a matrix of any order has ord*ord number of elements. Depending on the number of the row, i and the number of the column j, the formula is each element is determined by (x)+(j-1)+ (i-1)*ord.
  •  To flip elements along the first diagonal, where i=j, we simply replace each i and j with (ord+1-i) or (ord+1-j) as i=j. In replacing he elements, a temporary variable, temp proves to be useful. Note that only, half of the elements require to be moved. Hence the ord/2, where ord is the order of the matrix.
  • To flip elements along the second diagonal, where ord+1=i+j, we need to move half of the elements only. The other half will switch with them.However, the condition ord+1=i+j, is true for i less than or equal to ord/2 only when j is greater than or equal to ord/2. Then we move all elements in (i,j)  to (j,i), where i and j are dummy variables.
Easy Way to Build a 4x4 Magic Square. Note that the numbers circled red, the numbers circled green and the four purple squares also sum up to 34.
Now for the FORTRAN program,
! Magic square of order 4

program magicsqeven
    implicit none

    integer i,j,ord,x, mat(100,100),temp, sum1,sum2,sum3,sum4 !sum1 to sum4 are optional
    ord=4
    write(*,*) "The order of the magic square is", ord
    write (*,*) "Give the smallest number of the matrix"
    read (*,*) x
    write (*,*) "Producing a magic square beginning with number", x
   
    !To set blocks to their initial values
    do i=1, ord
          do j=1,ord
            mat(i,j)=(x+j-1)+ (i-1)*ord
        end do
    end do

    !To flip the first diagonal
    do i=1,ord/2
        do j=1,ord/2
              if (i==j)then
                temp=mat(i,j)
                   mat(i,j)=mat(ord+1-i,ord+1-i)
                mat(ord+1-i,ord+1-i)=temp
            end if
        end do
     end do

    !To flip the second diagonal
    do i=1,ord/2
        do j=ord/2,ord
            if ((i+j)==(ord+1))then
                 temp=mat(i,j)
                   mat(i,j)=mat(ord+1-i,i)
                mat(ord+1-i,i)=temp
            end if
        end do
    end do

    !To display results
    write (*,*) "The 4x4 magic square is displayed below:"
    do i=1,ord
        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 each row
        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 each column
        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 diagonal 1
        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 diagonal 2
        j=(ord+1-i)
        sum4=mat(i,j)+sum4
    end do
    write (*,*) "sum for diagonal 2 is", sum4

end program magicsqeven

Sample Output:

Output for 4x4 Magic Square beginning with 1162

No comments:

Post a Comment