14
The challenge
Output an array or string representation of Dürer's famous magic square:
that is,
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Some properties of this square, which can perhaps be exploited, are:
- It contains each integer from
1
to16
exactly once - The sum of each column or row, as well as the the sum of each of the two diagonals, is the same. This is the defining property of a magic square. The sum is the magic constant of the square.
- In addition, for this particular square, the sum of each of the four quadrants also equals the magic constant, as do the sum of the center four squares and the sum of the corner four squares.
Rules
Bultins that generate magic squares are not allowed (such as Matlab's magic
or Mathematica's MagicSquare
). Any other builtin can be used.
The code can be a program or a function.
There is no input.
The numbers must be in base 10. The output format is flexible as usual. Some possibilities are:
A nested array (either function output, or its string representation, with or without separators, any type of matching brackets):
[[16, 3, 2, 13], [5, 10, 11, 8], [9, 6, 7, 12], [4, 15, 14, 1]]
A 2D array:
{16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1}
An array of four strings, or a string consisting of four lines. The numbers may be right-aligned
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
or left-aligned
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
A string with two different separators for row and column, such as
16,3,2,13|5,10,11,8|9,6,7,12|4,15,14,1
The output format should clearly differentiate rows and columns. For example, it is not allowed to output a flat array, or a string with all numbers separated by spaces.
Code golf. Shortest wins.
Related – Luis Mendo – 2016-10-31T22:07:42.330
4Interestingly, the numbers 5, 8, 9 and 12 are in their (1-indexed) positions, 6, 7, 10 and 11 have been reflected vertically, 2, 3, 14 and 15 have been reflected horizontally and 1, 4, 13 and 16 have been rotated 180°. I doubt that will help anyone though. – Neil – 2016-10-31T22:38:42.820
2Possibly useful observation: if you decrement 1 from each number, you can generate the square by starting with the array
[15]
, then repeatedly concatenating it with its reverse with each item XORed by 13, 3, 8, and 15, respectively. – ETHproductions – 2016-10-31T22:41:08.7936This seems rather hard to compress in non-golfing languages. i think a larger magic square would have done better. – xnor – 2016-10-31T22:51:03.137
I'm waiting for the Mathematic answer:
Print[Dirac]
– Fund Monica's Lawsuit – 2016-11-01T07:41:21.707"Interger", heh... – Magic Octopus Urn – 2016-11-01T12:09:15.533
@carusocomputing Thanks, corrected :-) – Luis Mendo – 2016-11-01T12:20:56.320
I wonder if anyone will try the "Generate a 4x4 grid of randomly generated numbers until it's a magic square" approach and win. – Magic Octopus Urn – 2016-11-01T13:14:00.317
@Caruso With that approach you may produce a different magic square. But perhaps the other conditions do ensure uniqueness – Luis Mendo – 2016-11-01T13:24:18.287
1I'm fairly certain each rotation or reflection of the square would have the same properties. – Dennis – 2016-11-01T15:10:46.403
@LuisMendo Could check to see that [0,0] = 16 ;) – Magic Octopus Urn – 2016-11-01T16:37:36.347
Is there a performance requirement of the solutions? – Emigna – 2016-11-01T19:44:12.193
@Emigna No. If it theoretically works it's fine even if it takes a long time or a lot of memory – Luis Mendo – 2016-11-01T19:46:28.173
-1 because you banned
magic
=( – flawr – 2016-11-01T19:55:39.337