Magic squares of doubly even order: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|Java}}: added comment)
(added a light blue grid (magic square) to the task section header.)
Line 1: Line 1:
{{draft task}}
{{draft task}}
A [[wp:Magic_square|magic square]] of doubly even order has a size that is a multiple of four (e.g. 4, 8, 12).
<br>This means that the subsquares also have an even size, which plays a role in the construction.

<!-- As more computer programming languages will be added, they will "fill up" the space to the left of this light blue grid, and the first language entry will be the (normal) full width, so the big size is essential "free space". Gerard Schildberger. -->
{| class="wikitable" style="float:right;border: 4px solid blue; background:lightblue; color:black; margin-left:auto;margin-right:auto;text-align:center;width:22em;height:15em;table-layout:fixed;font-size:150%"
|-
|<big>'''1'''</big>||<big>'''2'''</big>||<big>'''62'''</big>||<big>'''61'''</big>||<big>'''60'''</big>||<big>'''59'''</big>||<big>'''7'''</big>||<big>'''8'''</big>
|-
|<big>'''9'''</big>||<big>'''10'''</big>||<big>'''54'''</big>||<big>'''53'''</big>||<big>'''52'''</big>||<big>'''51'''</big>||<big>'''15'''</big>||<big>'''16'''</big>
|-
|<big>'''48'''</big>||<big>'''47'''</big>||<big>'''19'''</big>||<big>'''20'''</big>||<big>'''21'''</big>||<big>'''22'''</big>||<big>'''42'''</big>||<big>'''41'''</big>
|-
|<big>'''40'''</big>||<big>'''39'''</big>||<big>'''27'''</big>||<big>'''28'''</big>||<big>'''29'''</big>||<big>'''30'''</big>||<big>'''34'''</big>||<big>'''33'''</big>
|-
|<big>'''32'''</big>||<big>'''31'''</big>||<big>'''35'''</big>||<big>'''36'''</big>||<big>'''37'''</big>||<big>'''38'''</big>||<big>'''26'''</big>||<big>'''25'''</big>
|-
|<big>'''24'''</big>||<big>'''23'''</big>||<big>'''43'''</big>||<big>'''44'''</big>||<big>'''45'''</big>||<big>'''46'''</big>||<big>'''18'''</big>||<big>'''17'''</big>
|-
|<big>'''49'''</big>||<big>'''50'''</big>||<big>'''14'''</big>||<big>'''13'''</big>||<big>'''12'''</big>||<big>'''11'''</big>||<big>'''55'''</big>||<big>'''56'''</big>
|-
|<big>'''57'''</big>||<big>'''58'''</big>||<big>'''6'''</big>||<big>'''5'''</big>||<big>'''4'''</big>||<big>'''3'''</big>||<big>'''63'''</big>||<big>'''64'''</big>
|}



A [[wp:Magic_square|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.


'''The task''': create a magic square of 8 x 8.
'''The task''': create a magic square of 8 x 8.

Revision as of 08:47, 17 March 2016

Magic squares of doubly even order is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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


The task: create a magic square of 8 x 8.

Cf.
See also

Java

<lang java>public class MagicSquareDoublyEven {

   public static void main(String[] args) {
       int n = 8;
       for (int[] row : magicSquareDoublyEven(n)) {
           for (int x : row)
               System.out.printf("%2s ", x);
           System.out.println();
       }
       System.out.printf("\nMagic constant: %d ", (n * n + 1) * n / 2);
   }
   static int[][] magicSquareDoublyEven(final int n) {
       if (n < 4 || n % 4 != 0)
           throw new IllegalArgumentException("base must be a positive "
                   + "multiple of 4");
       // pattern of count-up vs count-down zones
       int bits = 0b1001011001101001;
       int size = n * n;
       int mult = n / 4;
       int[][] result = new int[n][n];
       for (int i = 0; i < size; i++) {
           int bitsPos = (i % n) / mult + (i / (n * mult) * 4);
           int value = (bits & (1 << bitsPos)) != 0 ? i + 1 : size - i;
           result[i / n][i % n] = value;
       }
       return result;
   }

}</lang>

 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

Perl 6

See Magic squares/Perl 6

Output:

With a parameter of 8:

 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

The magic number is 260

REXX

<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. */ call gen /*generate a grid in numerical order. */ call diag /*mark numbers on both diagonals. */ call corn /* " " in small corner boxen. */ call midd /* " " in the middle " */ call swap /*swap positive numbers with highest #.*/ call show /*display the doubly even magic square.*/ call sum /* " " magic number for square. */ exit /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ o: parse arg ?; return n-?+1 /*calculate the "other" (right) column.*/ @: parse arg x,y; return abs(@.x.y) diag: do r=1 for n; @.r.r=-@(r,r); o=o(r); @.r.o=-@(r,o); end; return midd: do r=L to H; do c=L to H; @.r.c=-@(r,c); end; end; return gen: #=0; do r=1 for n; do c=1 for n; #=#+1; @.r.c=#; end; end; return show: #=0; do r=1 for n; $=; do c=1 for n; $=$ right(@(r,c),w); end; say $; end; return sum: #=0; do r=1 for n; #=#+@(r,1); end; say; say 'The magic number is: ' #; return max#: do a=n to 1 by -1; do b=n to 1 by -1; if @.a.b>0 then return; end; end /*──────────────────────────────────────────────────────────────────────────────────────*/ swap: do r=1 for n

               do c=1  for n;  if @.r.c<0  then iterate;  call max#  /*find max number.*/
               parse value  -@.a.b  (-@.r.c)    with    @.r.c  @.a.b /*swap two values.*/
               end  /*c*/
             end    /*r*/
     return

/*──────────────────────────────────────────────────────────────────────────────────────*/ corn: do r=1 for n; if r>s & r<=n-s then iterate

               do c=1  for n;  if c>s & c<=n-s  then iterate;  @.r.c=-@(r,c);  end  /*c*/
             end   /*r*/
     return</lang>

output   when using the default input:

  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

The magic number is:  260

zkl

Translation of: Java

<lang zkl>class MagicSquareDoublyEven{

  fcn init(n){ var result=magicSquareDoublyEven(n) }
  fcn toString{
     sink,n:=Sink(String),result.len();  // num collumns
     fmt:="%2s ";
     foreach row in (result)
        { sink.write(row.apply('wrap(n){ fmt.fmt(n) }).concat(),"\n") }
     sink.write("\nMagic constant: %d".fmt((n*n + 1)*n/2));
     sink.close();
  }
  fcn magicSquareDoublyEven(n){
     if (n<4 or n%4!=0 or n>16)

throw(Exception.ValueError("base must be a positive multiple of 4"));

     bits,size,mult:=0b1001011001101001, n*n, n/4;
     result:=n.pump(List(),n.pump(List(),0).copy);  // array[n,n] of zero
     foreach i in (size){

bitsPos:=(i%n)/mult + (i/(n*mult)*4); value:=(bits.bitAnd((2).pow(bitsPos))) and i+1 or size-i; result[i/n][i%n]=value;

     }
     result;
  }

} MagicSquareDoublyEven(8).println();</lang>

Output:
 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