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.



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



Task
Create a magic square of   8 × 8.



Related tasks
 Magic squares of odd order
 Magic squares of singly even order



See also

 Doubly Even Magic Squares (1728.org)


*        Magic squares of doubly even order  01/03/2017
MAGICSDB CSECT
         USING  MAGICSDB,R13
         B      72(R15)            skip save area
         DC     17F'0'             save area
         STM    R14,R12,12(R13)
         ST     R13,4(R15)
         ST     R15,8(R13)
         LR     R13,R15            end of prolog
         SR     R8,R8              k=0
         LA     R6,0               i=0
       DO WHILE=(C,R6,LE,=A(N-1))  do i=0 to n-1
         MVC    PG,=CL80' '          clear buffer
         LA     R9,PG                pgi=0
         LA     R7,0                 j=0
       DO WHILE=(C,R7,LE,=A(N-1))    do j=0 to n-1
         LR     R4,R7                  j
         SRDA   R4,32                  >>r5
         D      R4,MULT                /mult
         LR     R2,R5                  r2=j/mult
         LR     R4,R6                  i
         SRDA   R4,32                  >>r5
         D      R4,MULT                /mult
         SLA    R5,2                   r5=(i/mult)*4
         AR     R2,R5                  bitpos=j/mult+(i/mult)*4
         STC    R2,XSLL+3              number_of_shift=bitpos
         L      R5,=F'1'               1
         EX     0,XSLL                 r5=1<<bitpos  (SLL R5,bitpos)
         L      R4,BITS                bits
         NR     R4,R5                  bits and (1<<bitpos)
       IF LTR,R4,NZ,R4 THEN            if (bits and (1<<bitpos))<>0
         LA     R10,1(R8)                x=k+1
       ELSE     ,                      else
         L      R10,SIZE                 size
         SR     R10,R8                   x=size-k
       ENDIF    ,                      endif
         XDECO  R10,XDEC               edit x
         MVC    0(4,R9),XDEC+8         output x
         LA     R9,4(R9)               pgi+=4
         LA     R8,1(R8)               k++
         LA     R7,1(R7)               j++
       ENDDO    ,                    enddo j
         XPRNT  PG,L'PG              print buffer
         LA     R6,1(R6)             i++
       ENDDO    ,                  enddo i
         MVC    PG,=CL80'magic constant='
         L      R1,=A((N*N+1)*N/2) magicnum=(n*n+1)*n/2
         XDECO  R1,XDEC            edit magicnum
         MVC    PG+15(4),XDEC+8    output magicnum
         XPRNT  PG,L'PG            print buffer
         L      R13,4(0,R13)       epilog
         LM     R14,R12,12(R13)
         XR     R15,R15            rc=0
         BR     R14                exit
XSLL     SLL    R5,0               shift left logical
N        EQU    8                  <= input value n
SIZE     DC     A(N*N)             size=n*n
MULT     DC     A(N/4)             mult=n/4  (multiple of 4)
BITS     DC     XL2'0000',BL2'1001011001101001'  pattern
PG       DS     CL80               buffer
XDEC     DS     CL12               temp for xdeco
         YREGS
         END    MAGICSDB