Magic squares of doubly even order: Difference between revisions

m (→‎{{header|Phix}}: added syntax colouring the hard way)
Line 1,654:
24 122 123 21 20 126 127 17 16 130 131 13
133 11 10 136 137 7 6 140 141 3 2 144</pre>
 
=={{header|jq}}==
{{trans|Wren}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
<lang jq>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:
magicSquareDoublyEven[]
| lpad(2)
| join(" ");
 
8 | task, "\nMagic constant \((.*. + 1) * . / 2)"</lang>
{{out}}
<pre>
1 2 62 61 60 59 7 8
9 10 54 53 52 51 15 16
48 47 19 20 21 22 42 41
40 39 27 28 29 30 34 33
32 31 35 36 37 38 26 25
24 23 43 44 45 46 18 17
49 50 14 13 12 11 55 56
57 58 6 5 4 3 63 64
 
Magic constant 260
</pre>
 
 
=={{header|Julia}}==
2,442

edits