How to resolve the algorithm Magic squares of doubly even order step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Magic squares of doubly even order step by step in the jq programming language
Table of Contents
Problem Statement
A magic square is an N×N 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 doubly even order has a size that is a multiple of four (e.g. 4, 8, 12).
This means that the subsquares also have an even size, which plays a role in the construction.
Create a magic square of 8 × 8.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Magic squares of doubly even order step by step in the jq programming language
Source code in the jq programming language
def lpad($len):
def l: tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
if type == "array" then map(l) else l end;
def magicSquareDoublyEven:
if . < 4 or .%4 != 0 then "Base must be a positive multiple of 4" | error else . end
| . as $n
# pattern of count-up vs count-down zones
| [1,0,0,1,0,1,1,0,0,1,1,0,1,0,0,1] as $bits
| ($n * $n) as $size
| ($n / 4 | floor) as $mult # how many multiples of 4
| { i:0, result: null }
| reduce range(0; $n) as $r (.;
reduce range(0; $n) as $c (.;
( (($c/$mult)|floor) + (($r/$mult)|floor) * 4) as $bitPos
| .result[$r][$c] =
(if ($bits[$bitPos] != 0) then .i + 1 else $size - .i end)
| .i += 1 ) )
| .result ;
# Input: the order
def task:
. as $n
| (.*.|tostring|length+1) as $width
| (magicSquareDoublyEven[] | lpad($width) | join(" ")),
"\nMagic constant for order \($n): \(($n*$n + 1) * $n / 2)\n\n" ;
8, 12 | task
You may also check:How to resolve the algorithm String concatenation step by step in the GDScript programming language
You may also check:How to resolve the algorithm GUI enabling/disabling of controls step by step in the LiveCode programming language
You may also check:How to resolve the algorithm Speech synthesis step by step in the Mathematica/Wolfram Language programming language
You may also check:How to resolve the algorithm Solve a Hopido puzzle step by step in the Perl programming language
You may also check:How to resolve the algorithm N-smooth numbers step by step in the Nim programming language