How to resolve the algorithm Magic squares of odd order step by step in the Julia programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Magic squares of odd order step by step in the Julia 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 Julia programming language

This code implements a function called magicsquareodd that generates an odd-order magic square. A magic square is a square matrix where the sum of each row, column, and diagonal is the same.

The magicsquareodd function takes one argument, base, which must be an odd integer greater than or equal to 3. It starts by creating a square matrix filled with zeros and sets the initial position to the top-left corner (row 1, column 1). It then iterates through the numbers from 1 to the square of the base, assigning each number to the current position in the matrix.

The function uses a specific algorithm to determine the next position in the matrix. If the cell to the right of the current position is empty, the next position is to the right. If the cell to the right is not empty, the next position is one row down and one column to the left.

If the next position is outside the bounds of the matrix, the function wraps around to the beginning of the next row. This ensures that all cells in the matrix are filled.

Once the function has assigned all the numbers to the matrix, it returns the resulting magic square.

The main program calls the magicsquareodd function for odd values of base from 3 to 7 and prints the resulting magic squares.

Here is an example output for base = 5:

Magic square with size 5 - magic constant = 65
----------------------------------------------------
5  1  24  25  20
4  20  19  18  21
21  18  17  16  22
22  17  16  15  23
23  16  15  14  24

Source code in the julia programming language

# v0.6.0

function magicsquareodd(base::Int)
    if base & 1 == 0 || base < 3; error("base must be odd and >3") end

    square = fill(0, base, base)
    r, number = 1, 1
    size = base * base

    c = div(base, 2) + 1
    while number  size
        square[r, c] = number
        fr = r == 1 ? base : r - 1
        fc = c == base ? 1 : c + 1
        if square[fr, fc] != 0
            fr = r == base ? 1 : r + 1
            fc = c
        end
        r, c = fr, fc
        number += 1
    end

    return square
end

for n in 3:2:7
    println("Magic square with size $n - magic constant = ", div(n ^ 3 + n, 2))
    println("----------------------------------------------------")
    square = magicsquareodd(n)
    for i in 1:n
        println(square[i, :])
    end
    println()
end


  

You may also check:How to resolve the algorithm Character codes step by step in the CoffeeScript programming language
You may also check:How to resolve the algorithm Secure temporary file step by step in the Racket programming language
You may also check:How to resolve the algorithm Guess the number step by step in the BASIC programming language
You may also check:How to resolve the algorithm Pascal's triangle step by step in the IS-BASIC programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Lua programming language