A magic square is an arrangement of $$n^2$$ distinct numbers (i.e. each number is used only once) in a square $$n \times n$$ grid, where the numbers on each row, on each column, and the numbers on the main and the secondary diagonals all add up to the same number. The number of rows (and columns) $$n$$ is called the order of the square. The unique sum is called the magic constant or magic sum of the square. As an example, here is an order-4 magic square having magic sum 34.
16 | 3 | 2 | 13 |
5 | 10 | 11 | 8 |
9 | 6 | 7 | 12 |
4 | 15 | 14 | 1 |
This famous magic square occurs in Albrecht Dürer's engraving Melencolia I and is believed to be the first seen in European art. The sum 34 can be found in the rows, columns, diagonals, each of the quadrants, the center four squares, and the corner squares (of the $$4 \times 4$$ as well as the four contained $$3 \times 3$$ grids). This sum can also be found in the four outer numbers clockwise from the corners ($$3+8+14+9$$) and likewise the four counter-clockwise (the locations of four queens in the two solutions of the 4 queens puzzle), the two sets of four symmetrical numbers ($$2+8+9+15$$ and $$3+5+12+14$$), the sum of the middle two entries of the two outer columns and rows ($$5+9+8+12$$ and $$3+2+15+14$$), and in four kite or cross shaped quartets ($$3+5+11+15$$, $$2+10+8+14$$, $$3+9+7+15$$, and $$2+6+12+14$$). The two numbers in the middle of the bottom row give the date of the engraving: 1514. The numbers 1 and 4 at either side of the date correspond to the letters A and D which are the initials of the artist.
A heterosquare is an arrangement of $$n^2$$ distinct numbers (i.e. each number is used only once) in a square $$n \times n$$ grid, where the numbers on each row, on each column, and the numbers on the main and the secondary diagonals all add up to a distinct number. As an example, here is an order-4 heterosquare.
2 | 1 | 3 | 4 |
5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 |
An antimagic square is a heterosquare whose row, column and diagonal sums are consecutive integers (if arranged in ascending order). As an example, here is an order-4 antimagic square.
2 | 15 | 5 | 13 |
16 | 3 | 7 | 12 |
9 | 8 | 14 | 1 |
6 | 4 | 11 | 10 |
We represent a square $$n \times n$$ grid as a sequence (list or tuple) of $$n$$ elements, representing the $$n$$ rows of the grid (listed from top to bottom). Each row is itself represented as a sequence (list or tuple) of $$n$$ integers (int), representing the successive numbers on that row (listed from left to right).
Your task is to implement five functions that each take a square $$n \times n$$ grid as their argument:
A function numbers that returns a set containing the numbers (int) in the given grid.
A function sums that returns a set containing the sums (int) of all rows, columns and diagonals of the given grid.
A function ismagic that returns a Boolean value (bool) indicating whether the given grid is a magic square.
A function ishetero that returns a Boolean value (bool) indicating whether the given grid is a heterosquare.
A function isantimagic that returns a Boolean value (bool) indicating whether the given grid is an antimagic square.
>>> numbers([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
{1, 2, 3, 4, 5, 6, 7, 8, 9}
>>> numbers([[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
>>> numbers([[2, 15, 5, 13], [16, 3, 7, 12], [9, 8, 14, 1], [6, 4, 11, 10]])
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}
>>> sums([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
{15}
>>> sums([[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
{34, 35, 36, 40, 10, 42, 26, 58, 29, 31}
>>> sums([[2, 15, 5, 13], [16, 3, 7, 12], [9, 8, 14, 1], [6, 4, 11, 10]])
{32, 33, 34, 35, 36, 37, 38, 29, 30, 31}
>>> ismagic([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
True
>>> ismagic([[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
False
>>> ismagic([[2, 15, 5, 13], [16, 3, 7, 12], [9, 8, 14, 1], [6, 4, 11, 10]])
False
>>> ishetero([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
False
>>> ishetero([[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
True
>>> ishetero([[2, 15, 5, 13], [16, 3, 7, 12], [9, 8, 14, 1], [6, 4, 11, 10]])
True
>>> isantimagic([[2, 7, 6], [9, 5, 1], [4, 3, 8]])
False
>>> isantimagic([[2, 1, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]])
False
>>> isantimagic([[2, 15, 5, 13], [16, 3, 7, 12], [9, 8, 14, 1], [6, 4, 11, 10]])
True