How to resolve the algorithm Magic squares of singly even order step by step in the Wren programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Magic squares of singly even order step by step in the Wren programming language
Table of Contents
Problem Statement
A magic square is an NxN square matrix whose numbers consist of consecutive numbers arranged so that the sum of each row and column, and both diagonals are equal to the same sum (which is called the magic number or magic constant). A magic square of singly even order has a size that is a multiple of 4, plus 2 (e.g. 6, 10, 14). This means that the subsquares have an odd size, which plays a role in the construction.
Create a magic square of 6 x 6.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magic squares of singly even order step by step in the Wren programming language
Source code in the wren programming language
import "/fmt" for Fmt
var magicSquareOdd = Fn.new { |n|
if (n < 3 || n%2 == 0) Fiber.abort("Base must be odd and > 2")
var value = 1
var gridSize = n * n
var c = (n/2).floor
var r = 0
var result = List.filled(n, null)
for (i in 0...n) result[i] = List.filled(n, 0)
while (value <= gridSize) {
result[r][c] = value
if (r == 0) {
if (c == n - 1) {
r = r + 1
} else {
r = n - 1
c = c + 1
}
} else if (c == n - 1) {
r = r - 1
c = 0
} else if (result[r - 1][c + 1] == 0) {
r = r - 1
c = c + 1
} else {
r = r + 1
}
value = value + 1
}
return result
}
var magicSquareSinglyEven = Fn.new { |n|
if (n < 6 || (n - 2) % 4 != 0) {
Fiber.abort("Base must be a positive multiple of 4 plus 2")
}
var size = n * n
var halfN = n / 2
var subSquareSize = size / 4
var subSquare = magicSquareOdd.call(halfN)
var quadrantFactors = [0, 2, 3, 1]
var result = List.filled(n, null)
for (i in 0...n) result[i] = List.filled(n, 0)
for (r in 0...n) {
for (c in 0...n) {
var quadrant = (r/halfN).floor * 2 + (c/halfN).floor
result[r][c] = subSquare[r % halfN][c % halfN]
result[r][c] = result[r][c] + quadrantFactors[quadrant] * subSquareSize
}
}
var nColsLeft = (halfN/2).floor
var nColsRight = nColsLeft - 1
for (r in 0...halfN) {
for (c in 0...n) {
if (c < nColsLeft || c >= n - nColsRight || (c == nColsLeft && r == nColsLeft)) {
if (c != 0 || r != nColsLeft) {
var tmp = result[r][c]
result[r][c] = result[r + halfN][c]
result[r + halfN][c] = tmp
}
}
}
}
return result
}
var n = 6
for (ia in magicSquareSinglyEven.call(n)) {
for (i in ia) Fmt.write("$2d ", i)
System.print()
}
System.print("\nMagic constant %((n * n + 1) * n / 2)")
You may also check:How to resolve the algorithm Accumulator factory step by step in the Brat programming language
You may also check:How to resolve the algorithm Archimedean spiral step by step in the Delphi programming language
You may also check:How to resolve the algorithm Anonymous recursion step by step in the Falcon programming language
You may also check:How to resolve the algorithm Date format step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Synchronous concurrency step by step in the Ruby programming language