Magic squares of doubly even order: Difference between revisions

m
imported>Arakov
 
(8 intermediate revisions by 5 users not shown)
Line 42:
{{trans|Java}}
 
<langsyntaxhighlight lang="11l">F magicSquareDoublyEven(n)
V bits = 1001'0110'0110'1001b
V size = n * n
Line 61:
print(‘#2 ’.format(x), end' ‘’)
print()
print("\nMagic constant: "((n * n + 1) * n I/ 2))</langsyntaxhighlight>
 
{{out}}
Line 79:
=={{header|360 Assembly}}==
{{trans|Java}}
<langsyntaxhighlight lang="360asm">* Magic squares of doubly even order 01/03/2017
MAGICSDB CSECT
USING MAGICSDB,R13
Line 141:
XDEC DS CL12 temp for xdeco
YREGS
END MAGICSDB</langsyntaxhighlight>
{{out}}
<pre>
Line 157:
=={{header|ALGOL 60}}==
{{works with|A60}}
<langsyntaxhighlight lang="algol60">begin
comment Magic squares of doubly even order - 10/02/2021;
integer array pattern[1:4,1:4];
Line 181:
end;
outstring(1,"magic constant ="); outinteger(1,(s+1)*n div 2)
end </langsyntaxhighlight>
{{out}}
<pre>
Line 199:
{{Trans|ALGOL 60}}
Using a procedure to generate the square for easier re-use.
<langsyntaxhighlight lang="algol68">BEGIN # Magic squares of doubly even order #
PROC doubly even magic square = ( INT n )[,]INT:
IF n MOD 4 /= 0 THEN
Line 247:
print( ( "magic constant = ", whole( ( ( ( order * order ) + 1 ) * order ) OVER 2, 0 ), newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 274:
133 134 135 9 8 7 6 5 4 142 143 144
magic constant = 870
</pre>
 
=={{header|Amazing Hopper}}==
{{Trans|C++}}
<syntaxhighlight lang="c">
/*
Magic squares of doubly even order. Rosettacode.org
By Mr. Dalien.
*/
 
#include <basico.h>
 
#proto cuadradomágico(_X_)
#synon _cuadradomágico generarcuadradomágico
 
principal {
decimales '0', fijar separador 'NULO'
malla de bits(bits,'1;0;0;1','0;1;1;0','0;1;1;0','1;0;0;1')
borrar pantalla
iterar para( i=1; n=4, #(i<=3), ++i; #(n=4*i) )
dim( #(n*n) ) matriz de ceros 'sqr'
generar cuadrado mágico (n);
ir a sub ( meter en tabla, sqr, #(n+1) ), para 'sqr'
imprimir '"Magic square order ",n,\
"\nMagic constant : ",#((n * n + 1) * n / 2),\
NL,sqr,NL,NL'
luego limpiar 'sqr'
siguiente
terminar
}
 
subrutinas
 
sub( meter en tabla, s, n ) {
i=n,rareti(i, 'i')
retener 'n', insertar columnas en 's'
insertar filas en 's'
#basic{
s[1:2:2*n-1,1:_end_] = "---"
s[1:_end_, 1:2:2*n-1] = "|"
s[1:2:_end_,1:2:_end_] = "+"
}
retornar 's'
}
 
cuadrado mágico ( n )
iterar para ( cr=0;i=0, #(cr<n), ++cr )
iterar para ( cc=0, #(cc<n), ++cc;++i )
#( sqr[ (cc+n*cr)+1 ] = (bits[(cr%4+1),(cc%4+1)]) ? (i+1) : (n^2-i); )
siguiente
siguiente
#( sqr = lpad(" ",3,string(sqr)))
redim ( sqr, n, n )
retornar
</syntaxhighlight>
{{out}}
<pre>
Magic square order 4
Magic constant : 34
+---+---+---+---+
| 1| 15| 14| 4|
+---+---+---+---+
| 12| 6| 7| 9|
+---+---+---+---+
| 8| 10| 11| 5|
+---+---+---+---+
| 13| 3| 2| 16|
+---+---+---+---+
 
 
Magic square order 8
Magic constant : 260
+---+---+---+---+---+---+---+---+
| 1| 63| 62| 4| 5| 59| 58| 8|
+---+---+---+---+---+---+---+---+
| 56| 10| 11| 53| 52| 14| 15| 49|
+---+---+---+---+---+---+---+---+
| 48| 18| 19| 45| 44| 22| 23| 41|
+---+---+---+---+---+---+---+---+
| 25| 39| 38| 28| 29| 35| 34| 32|
+---+---+---+---+---+---+---+---+
| 33| 31| 30| 36| 37| 27| 26| 40|
+---+---+---+---+---+---+---+---+
| 24| 42| 43| 21| 20| 46| 47| 17|
+---+---+---+---+---+---+---+---+
| 16| 50| 51| 13| 12| 54| 55| 9|
+---+---+---+---+---+---+---+---+
| 57| 7| 6| 60| 61| 3| 2| 64|
+---+---+---+---+---+---+---+---+
 
 
Magic square order 12
Magic constant : 870
+---+---+---+---+---+---+---+---+---+---+---+---+
| 1|143|142| 4| 5|139|138| 8| 9|135|134| 12|
+---+---+---+---+---+---+---+---+---+---+---+---+
|132| 14| 15|129|128| 18| 19|125|124| 22| 23|121|
+---+---+---+---+---+---+---+---+---+---+---+---+
|120| 26| 27|117|116| 30| 31|113|112| 34| 35|109|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 37|107|106| 40| 41|103|102| 44| 45| 99| 98| 48|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 49| 95| 94| 52| 53| 91| 90| 56| 57| 87| 86| 60|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 84| 62| 63| 81| 80| 66| 67| 77| 76| 70| 71| 73|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 72| 74| 75| 69| 68| 78| 79| 65| 64| 82| 83| 61|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 85| 59| 58| 88| 89| 55| 54| 92| 93| 51| 50| 96|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 97| 47| 46|100|101| 43| 42|104|105| 39| 38|108|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 36|110|111| 33| 32|114|115| 29| 28|118|119| 25|
+---+---+---+---+---+---+---+---+---+---+---+---+
| 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|AppleScript}}==
{{Trans|JavaScript}}
<langsyntaxhighlight AppleScriptlang="applescript">-- MAGIC SQUARE OF DOUBLY EVEN ORDER -----------------------------------------
 
-- magicSquare :: Int -> [[Int]]
Line 531 ⟶ 652:
end tell
return v
end |until|</langsyntaxhighlight>
{{Out}}
magic(8)
Line 557 ⟶ 678:
{{trans|C#}}
Since standard awk does not support bitwise operators, we provide the function countup to do the magic.
<langsyntaxhighlight AWKlang="awk"># Usage: GAWK -f MAGIC_SQUARES_OF_DOUBLY_EVEN_ORDER.AWK
BEGIN {
n = 8
Line 596 ⟶ 717:
bitpos = int(c / mult) + int(r / mult) * 4 + 1
return substr(pattern, bitpos, 1) + 0
}</langsyntaxhighlight>
 
{{out}}
Line 613 ⟶ 734:
The size, ''N'', is specified by the first value on the stack. The algorithm is loosely based on the [[Magic_squares_of_doubly_even_order#Java|Java]] implementation.
 
<langsyntaxhighlight lang="befunge">8>>>v>10p00g:*1-*\110g2*-*+1+.:00g%!9+,:#v_@
p00:<^:!!-!%3//4g00%g00\!!%3/*:g00*4:::-1<*:</langsyntaxhighlight>
 
{{out}}
Line 628 ⟶ 749:
=={{header|C}}==
Takes number of rows from command line, prints out usage on incorrect invocation.
<syntaxhighlight lang="c">
<lang C>
#include<stdlib.h>
#include<ctype.h>
Line 691 ⟶ 812:
return 0;
}
</syntaxhighlight>
</lang>
Invocation and Output :
<pre>
Line 713 ⟶ 834:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang="csharp">using System;
 
namespace MagicSquareDoublyEven
Line 757 ⟶ 878:
}
}
}</langsyntaxhighlight>
<pre> 1 2 62 61 60 59 7 8
9 10 54 53 52 51 15 16
Line 770 ⟶ 891:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <iostream>
#include <sstream>
#include <iomanip>
Line 822 ⟶ 943:
s.display();
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 840 ⟶ 961:
=={{header|D}}==
{{trans|Java}}
<langsyntaxhighlight Dlang="d">import std.stdio;
 
void main() {
Line 875 ⟶ 996:
 
return result;
}</langsyntaxhighlight>
 
{{out}}
Line 888 ⟶ 1,009:
 
Magic constant: 260</pre>
 
=={{header|EDSAC order code}}==
<syntaxhighlight lang="edsac">
[Magic squares of doubly even order, for Rosetta Code.
EDSAC program, Initial Orders 2.]
[====================================================================================
Certain cells in the square are marked, such that if a cell is marked then so is
its reflection in the centre point of the square. Cells are numbered 1, 2, 3 ...
from left to right and top to bottom, but if a cell is marked it is swapped with its
reflection. Two marking methods are used, illustrated below for an 8x8 square.
+ o o + + o o + + + o o o o + +
o + + o o + + o + + o o o o + +
o + + o o + + o o o + + + + o o
+ o o + + o o + o o + + + + o o o = unmarked
+ o o + + o o + o o + + + + o o + = marked
o + + o o + + o o o + + + + o o
o + + o o + + o + + o o o o + +
+ o o + + o o + + + o o o o + +
Diagonal method Rectangle method
====================================================================================]
[Arrange the storage]
T45K P56F [H parameter: subroutine to print string]
T46K P100F [N parameter: subroutine to print number]
T47K P204F [M parameter: main routine]
T54K P200F [C parameter: constants read by subroutine R2]
 
[Library subroutine R2: reads integers from tape and can then be overwritten.]
GK T20F VD L8F A40D UD TF I40F A40F S39F G@ S2F G23F A5@ T5@ E4@ E13Z
T#C [tell R2 where to store values]
13743895347F8589934592#TZ
[C parameter: masks read in by subroutine R2, not by the regular loader]
[0] PF PF [diagonal method, binary 01100110011001100110011001100110011]
[2] PF PF [rectangle method, binary 01000000000000000000000000000000000]
 
[M parameter Main routine + high-level subroutine]
E25K TM GK
[35-bit values, must be at even address]
[0] PF PF [initial value of x mask]
[2] PF [x-mask, low 17 bits]
[3] PF [x-mask, high 17 bits]
[4] PF [y-mask, low 17 bits]
[5] PF [y-mask, high 17 bits]
[17-bit values]
[6] PF [sign bit from y mask]
[7] PF [m, input by user]
[8] PF [n = 4*m = order of magic square]
[9] PF [n^2 + 1]
[10] PF [negative counter for x-values (columns)]
[11] PF [negative counter for y-values (rows)]
[12] PF [current entry 1, 2, 3, ...]
[13] PD [constant 1]
[14] K4096F [null]
[15] !F [space]
[16] @F [carriage return]
[17] &F [line feed]
[18] P10F [to check for user dialling '0']
[Strings to be printed]
[19] K2048FMFAFGFIFCF!FSFQFUFAFRFEF!FOFFF!FOFRFDFEFRF!F#FRF*FMF@F&FK4096F
[49] K2048FDFIFAFLF!FMF!F#FKFPF!F*FTFOF!FCFAFNFCFEFLF#FLF!FK4096F
[75] K2048FDFIFAFGFOFNFAFLF!FMFEFTFHFOFDF#FCF@F&FK4096F
[96] K2048FRFEFCFTFAFNFGFLFEF!FMFEFTFHFOFDF#FCF@F&FK4096F
 
[Enter with acc = 0]
[118] A118@ GH A19@ [print 'MAGIC SQUARE OF ORDER 4M']
[121] A121@ GH A49@ [print 'DIAL M (0 TO CANCEL)']
ZF [halt machine; restarts when user dials a number]
[Here acc holds number of pulses in address field]
S18@ E175@ [exit if dialled '0' (10 pulses)]
A18@ [restore acc after test; m is in address field]
L512F [shift 11 left for printing]
UF [temp to 0F]
OF O16@ O17@ [print m followed by CR, LF]
R1024F [shift 12 right, m is now right-justified]
U7@ [store m]
L1F T8@ [shift 2 left and store n = 4*m]
H8@ V8@ [acc := n^2]
L64F L64F [shift 16 left to adjust scaling after mult]
A13@ T9@ [store n^2 + 1 = sum of a cell and its reflection]
[143] A143@ GH A75@ [print 'DIAGONAL METHOD:']
A#C [acc := diagonal mask]
U#@ [store as x-mask at start of each row]
T4#@ [also as initial value of y-mask]
[149] A149@ G177@ [call subroutine to print magic square]
[151] A151@ GH A96@ [print 'RECTANGLE METHOD:']
A2#C U4D T6D [copy rectangke mask to 4D and 6D]
S7@ [initialize negative counter to -m]
[158] TF [loop: update negative counter in 0F]
A4D RD T4D [shift 4D 1 right]
A6D R2F T6D [shift 6D 3 right]
AF A13@ [inc negative counter]
G158@ [loop back till done m times]
A4D S6D L1F [acc := 4D - 6D, then 2 left]
[Mask in binary is now 0 (m times) 1 (2*m times) 0...0]
U#@ [store as x-mask at start of each row]
T4#@ [also as initial value of y-mask]
[173] A173@ G177@ [call subroutine to print magic square]
[175] O14@ [done; print null to flush teleprinter buffer]
ZF [halt machine]
 
[Subroutine to print magic square after x- and y-mask have been initialized.]
[It's assumed that strings printed by caller leave teleprinter in figures mode.]
[177] A3F T220@ [plant return link as usual]
A15@ T1F [space replaces leading 0 when printing]
T12@ [initialize cell entry to 0]
S8@ [initialize negative counter of rows to -n]
[Start of row]
[183] T11@ [update negative counter of rows]
A#@ T2#@ [reset x-mask for start of row]
H14@ C5@ T6@ [isolate sign bit of y-mask]
S8@ [initialize negative counter of columns to -n]
[Next cell in this row.
Cell is considered marked if sign bits in x- and y-masks are equal.
Or could say marked if sign bits are unequal; would also give a magic square.]
[190] T10@ [update negative counter of columns]
A12@ A13@ T12@ [inc cell entry]
A3@ A6@ [compare signs in x- and y-masks]
E200@ [jump if equal (or could replace E by G)]
TF [clear acc]
A12@ [acc := entry]
E203@ [join common code]
[200] TF [clear acc]
A9@ S12@ [acc := complement of entry]
[203] TF [to 0F for printing]
[204] A204@ GN [print number]
A2#@ LD T2#@ [shift x-mask 1 left]
A10@ A13@ [inc negative counter of cells]
G190@ [loop till row is complete]
[End of row]
O16@ O17@ [print CR, LF]
A4#@ LD T4#@ [shift y-mask 1 left]
A11@ A13@ [inc negative counter of rows]
G183@ [loop till magic square is complete]
[220] ZF [(planted) jump back to caller]
 
[H parameter: Subroutine to print a string.]
E25K TH
[Input: A order for first character must follow subroutine call (G order)
String is terminated with EDSAC null, which is sent to the teleprinter.]
GKA18@U17@S19@T4@AFT6@AFUFOFE12@A20@G16@TFA6@A2FG5@TFZFU3FU1FK2048F
 
[N parameter: Subroutine to print non-negative 17-bit integer.]
E25K TN
[Parameters: 0F = integer to be printed (not preserved)
1F = character for leading zero (preserved)
Workspace: 4F..7F, 38 locations]
GKA3FT34@A1FT7FS35@T6FT4#FAFT4FH36@V4FRDA4#FR1024FH37@E23@O7FA2F
T6FT5FV4#FYFL8FT4#FA5FL1024FUFA6FG16@OFTFT7FA6FG17@ZFP4FZ219DTF
 
[M parameter again]
E25K TM GK
E118Z [define entry point]
PF [acc = 0 on entry]
 
</syntaxhighlight>
{{out}}
<pre>
MAGIC SQUARE OF ORDER 4M
DIAL M (0 TO CANCEL) 2
DIAGONAL METHOD:
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1
RECTANGLE METHOD:
64 63 3 4 5 6 58 57
56 55 11 12 13 14 50 49
17 18 46 45 44 43 23 24
25 26 38 37 36 35 31 32
33 34 30 29 28 27 39 40
41 42 22 21 20 19 47 48
16 15 51 52 53 54 10 9
8 7 59 60 61 62 2 1
</pre>
 
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<langsyntaxhighlight lang="elena">import system'routines;
import extensions;
import extensions'routines;
Line 899 ⟶ 1,197:
{
if(n < 4 || n.mod(4) != 0)
{ InvalidArgumentException.new:("base must be a positive multiple of 4").raise() };
int bits := 09669h;
Line 907 ⟶ 1,205:
var result := IntMatrix.allocate(n,n);
int i := 0;
for (int r := 0,; r < n,; r += 1)
{
for(int c := 0,; c < n,; c += 1,; i += 1)
{
int bitPos := c / mult + (r / mult) * 4;
result[r][c] := ((bits && (1 $shl bitPos)) != 0).iif(i+1,size - i)
}
};
Line 927 ⟶ 1,225:
console.printLine().printLine("Magic constant: ",(n * n + 1) * n / 2)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 943 ⟶ 1,241:
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Magic_square do
def doubly_even(n) when rem(n,4)!=0, do: raise ArgumentError, "must be even, but not divisible by 4."
def doubly_even(n) do
Line 969 ⟶ 1,267:
end
 
Magic_square.doubly_even(8)</langsyntaxhighlight>
 
{{out}}
Line 984 ⟶ 1,282:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays combinators.short-circuit formatting fry
generalizations kernel math math.matrices prettyprint sequences
;
Line 1,031 ⟶ 1,329:
] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,069 ⟶ 1,367:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 18-03-2016
' compile with: fbc -s console
' doubly even magic square 4, 8, 12, 16...
Line 1,188 ⟶ 1,486:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>Single even magic square size: 8*8
Line 1,203 ⟶ 1,501:
 
=={{header|Go}}==
<langsyntaxhighlight Golang="go">package main
 
import (
Line 1,254 ⟶ 1,552:
fmt.Printf("\nMagic Constant: %d\n", magicConstant)
}
</syntaxhighlight>
</lang>
{{Out}}
<pre> 1 2 62 61 60 59 7 8
Line 1,269 ⟶ 1,567:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List (transpose, unfoldr, intercalate)
import Data.List.Split (chunksOf)
import Data.Bool (bool)
Line 1,321 ⟶ 1,619:
putStrLn $ unlines (table " " (fmap show <$> test))
print $ checked test
putStrLn []</langsyntaxhighlight>
{{Out}}
<pre> 1 15 14 4
Line 1,361 ⟶ 1,659:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
masksq=: >:@#@, | _1&^ * 1 + i.@$
pat4=: ,:(+.|.)=i.4
mask=: ,/@(,./"3@$&pat4)@] ,~ % 4:
demsq=: masksq@mask
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,383 ⟶ 1,681:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class MagicSquareDoublyEven {
 
public static void main(String[] args) {
Line 1,415 ⟶ 1,713:
return result;
}
}</langsyntaxhighlight>
 
<pre> 1 2 62 61 60 59 7 8
Line 1,432 ⟶ 1,730:
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 1,614 ⟶ 1,912:
// MAIN ------------------------------------------------
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Order: 4
Line 1,660 ⟶ 1,958:
'''Works with gojq, the Go implementation of jq'''
 
<langsyntaxhighlight lang="jq">def lpad($len):
def l: tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
if type == "array" then map(l) else l end;
Line 1,687 ⟶ 1,985:
"\nMagic constant for order \($n): \(($n*$n + 1) * $n / 2)\n\n" ;
 
8, 12 | task</langsyntaxhighlight>
{{out}}
<pre>
Line 1,720 ⟶ 2,018:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Printf
 
function magicsquaredoubleeven(order::Int)
Line 1,747 ⟶ 2,045:
end
println()
end</langsyntaxhighlight>
 
{{out}}
Line 1,785 ⟶ 2,083:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.0
 
fun magicSquareDoublyEven(n: Int): Array<IntArray> {
Line 1,813 ⟶ 2,111:
}
println("\nMagic constant ${(n * n + 1) * n / 2}")
}</langsyntaxhighlight>
 
{{out}}
Line 1,835 ⟶ 2,133:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import bitops, sequtils, strutils
 
type Square = seq[seq[int]]
Line 1,867 ⟶ 2,165:
let n = 8
echo magicSquareDoublyEven(n)
echo "Magic constant = ", n * (n * n + 1) div 2</langsyntaxhighlight>
 
{{out}}
Line 1,884 ⟶ 2,182:
 
A magic one-liner:
<langsyntaxhighlight lang="parigp">magicsquare(n)=matrix(n,n,i,j,k=i+j*n-n;if(bitand(38505,2^((j-1)%4*4+(i-1)%4)),k,n*n+1-k))</langsyntaxhighlight>
 
Output:<pre>magicsquare(8)
Line 1,909 ⟶ 2,207:
 
See [[Magic_squares/Perl|Magic squares/Perl]] for a general magic square generator.
<lang perl></lang>
 
=={{header|Phix}}==
{{trans|C++}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span>
Line 1,954 ⟶ 2,251:
<span style="color: #000000;">check</span><span style="color: #0000FF;">(</span><span style="color: #000000;">square</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,970 ⟶ 2,267:
=={{header|PureBasic}}==
{{trans|FreeBasic}}
<langsyntaxhighlight PureBasiclang="purebasic">Procedure.i MagicN(n.i)
ProcedureReturn n*(n*n+1)/2
EndProcedure
Line 2,037 ⟶ 2,334:
DblEvenMagicSquare(n)
n=0
ForEver</langsyntaxhighlight>
{{out}}<pre>Input [4,8,12..n] (0=Exit)
>8
Line 2,054 ⟶ 2,351:
=={{header|Python}}==
===Procedural===
<langsyntaxhighlight lang="python">
def MagicSquareDoublyEven(order):
sq = [range(1+n*order,order + (n*order)+1) for n in range(order) ]
Line 2,082 ⟶ 2,379:
 
printsq(MagicSquareDoublyEven(8))
</syntaxhighlight>
</lang>
{{out}}<pre>
1 2 62 61 60 59 7 8
Line 2,101 ⟶ 2,398:
Generating test results and a magic square in the form of a wiki table:
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Magic squares of doubly even order'''
 
from itertools import chain, repeat
Line 2,271 ⟶ 2,568:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Row sums: [260, 260, 260, 260, 260, 260, 260, 260]
Line 2,355 ⟶ 2,652:
Translation of the magic square code example from [https://math.nist.gov/javanumerics/jama/ Jama], which is released to the public domain. This includes all three cases.
 
<langsyntaxhighlight Rlang="r">magic <- function(n) {
if (n %% 2 == 1) {
p <- (n + 1) %/% 2 - 2
Line 2,377 ⟶ 2,674:
a
}
}</langsyntaxhighlight>
 
'''Example'''
Line 2,430 ⟶ 2,727:
"Marked" numbers &nbsp; (via the &nbsp; '''diag''' &nbsp; subroutine) &nbsp; indicate that those (sequentially generated) numbers don't get
<br>swapped &nbsp; (and thusly, stay in place in the magic square).
<langsyntaxhighlight lang="rexx">/*REXX program constructs a magic square of doubly even sides (a size divisible by 4).*/
n= 8; s= n%4; L= n%2-s+1; w= length(n**2) /*size; small sq; low middle; # width*/
@.= 0; H= n%2+s /*array default; high middle. */
Line 2,460 ⟶ 2,757:
do c=1 for n; if c>s & c<=n-s then iterate; @.r.c= -@(r,c) /*negate*/
end /*c*/
end /*r*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 2,477 ⟶ 2,774:
=={{header|Ruby}}==
 
<langsyntaxhighlight lang="ruby">def double_even_magic_square(n)
raise ArgumentError, "Need multiple of four" if n%4 > 0
block_size, max = n/4, n*n
Line 2,494 ⟶ 2,791:
end
 
puts to_string(double_even_magic_square(8))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,508 ⟶ 2,805:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::env;
 
fn main() {
Line 2,544 ⟶ 2,841:
println!();
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,561 ⟶ 2,858:
=={{header|Scala}}==
{{Out}}Best seen running in your browser either by [https://scalafiddle.io/sf/bdTcGF3/0 ScalaFiddle (ES aka JavaScript, non JVM)] or [https://scastie.scala-lang.org/gLhkwHHlRO6rPXg9U7MDzg Scastie (remote JVM)].
<langsyntaxhighlight Scalalang="scala">object MagicSquareDoublyEven extends App {
private val n = 8
 
Line 2,583 ⟶ 2,880:
println(f"---%nMagic constant: ${(n * n + 1) * n / 2}%d")
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="ruby">func double_even_magic_square(n) {
assert(n%4 == 0, "Need multiple of four")
var (bsize, max) = (n/4, n*n)
Line 2,602 ⟶ 2,899:
}
 
say format_matrix(double_even_magic_square(8))</langsyntaxhighlight>
{{out}}
<pre>
Line 2,619 ⟶ 2,916:
{{trans|R}}
 
<langsyntaxhighlight lang="stata">mata
function magic(n) {
if (mod(n,2)==1) {
Line 2,652 ⟶ 2,949:
return(a)
}
end</langsyntaxhighlight>
 
<pre>. mata magic(8)
Line 2,669 ⟶ 2,966:
=={{header|VBScript}}==
{{trans|Java}}
<langsyntaxhighlight lang="vb">' Magic squares of doubly even order
n=8 'multiple of 4
pattern="1001011001101001"
Line 2,686 ⟶ 2,983:
wscript.echo l
Next 'r
wscript.echo "Magic constant=" & (n*n+1)*n/2</langsyntaxhighlight>
{{out}}
<pre>Magic square : 8 x 8
Line 2,701 ⟶ 2,998:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">Module MagicSquares
 
Function MagicSquareDoublyEven(n As Integer) As Integer(,)
Line 2,749 ⟶ 3,046:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1 2 62 61 60 59 7 8
Line 2,765 ⟶ 3,062:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Conv, Fmt
 
var magicSquareDoublyEven = Fn.new { |n|
Line 2,792 ⟶ 3,089:
System.print()
}
System.print("\nMagic constant %((n * n + 1) * n / 2)")</langsyntaxhighlight>
 
{{out}}
Line 2,810 ⟶ 3,107:
=={{header|zkl}}==
{{trans|Java}}
<langsyntaxhighlight lang="zkl">class MagicSquareDoublyEven{
fcn init(n){ var result=magicSquareDoublyEven(n) }
fcn toString{
Line 2,834 ⟶ 3,131:
}
}
MagicSquareDoublyEven(8).println();</langsyntaxhighlight>
{{out}}
<pre>
Anonymous user