How to resolve the algorithm Magic squares of odd order step by step in the Fortran programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Magic squares of odd order step by step in the Fortran programming language
Table of Contents
Problem Statement
A magic square is an NxN square matrix whose numbers (usually integers) consist of consecutive numbers arranged so that the sum of each row and column, and both long (main) diagonals are equal to the same sum (which is called the magic number or magic constant). The numbers are usually (but not always) the first N2 positive integers. A magic square whose rows and columns add up to a magic number but whose main diagonals do not, is known as a semimagic square.
For any odd N, generate a magic square with the integers 1 ──► N, and show the results here.
Optionally, show the magic number.
You should demonstrate the generator by showing at least a magic square for N = 5.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magic squares of odd order step by step in the Fortran programming language
Source code in the fortran programming language
program Magic_Square
implicit none
integer, parameter :: order = 15
integer :: i, j
write(*, "(a, i0)") "Magic Square Order: ", order
write(*, "(a)") "----------------------"
do i = 1, order
do j = 1, order
write(*, "(i4)", advance = "no") f1(order, i, j)
end do
write(*,*)
end do
write(*, "(a, i0)") "Magic number = ", f2(order)
contains
integer function f1(n, x, y)
integer, intent(in) :: n, x, y
f1 = n * mod(x + y - 1 + n/2, n) + mod(x + 2*y - 2, n) + 1
end function
integer function f2(n)
integer, intent(in) :: n
f2 = n * (1 + n * n) / 2
end function
end program
You may also check:How to resolve the algorithm 100 doors step by step in the Ada programming language
You may also check:How to resolve the algorithm Old lady swallowed a fly step by step in the 11l programming language
You may also check:How to resolve the algorithm Left factorials step by step in the Quackery programming language
You may also check:How to resolve the algorithm Damm algorithm step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Van der Corput sequence step by step in the C programming language