Magic squares of doubly even order

From Rosetta Code
Revision as of 01:50, 17 March 2016 by Thundergnat (talk | contribs) (→‎{{header|Perl 6}}: Generalize the magic squares code and link back)
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.

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");
       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

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