Magic squares of odd order: Difference between revisions
Content added Content deleted
(Add Seed7 example) |
|||
Line 829: | Line 829: | ||
putStrLn $ display $ magicNumber x |
putStrLn $ display $ magicNumber x |
||
</lang> |
</lang> |
||
=={{header|Mathematica}}== |
|||
Rotate rows and columns of the initial matrix with rows filled in order 1 2 3 .... N^2 |
|||
Method from http://www.jsoftware.com/papers/eem/magicsq.htm |
|||
<lang Mathematica> |
|||
rp[v_, pos_] := RotateRight[v, (Length[v] + 1)/2 - pos]; |
|||
rho[m_] := MapIndexed[rp, m]; |
|||
magic[n_] := |
|||
rho[Transpose[rho[Table[i*n + j, {i, 0, n - 1}, {j, 1, n}]]]]; |
|||
square = magic[11] // Grid |
|||
Print["Magic number is ", Total[square[[1, 1]]]] |
|||
</lang> |
|||
Output (alignment lost in translation to text): |
|||
{68, 80, 92, 104, 116, 7, 19, 31, 43, 55, 56}, |
|||
{81, 93, 105, 117, 8, 20, 32, 44, 45, 57, 69}, |
|||
{94, 106, 118, 9, 21, 33, 34, 46, 58, 70, 82}, |
|||
{107, 119, 10, 22, 23, 35, 47, 59, 71, 83, 95}, |
|||
{120, 11, 12, 24, 36, 48, 60, 72, 84, 96, 108}, |
|||
{1, 13, 25, 37, 49, 61, 73, 85, 97, 109, 121}, |
|||
{14, 26, 38, 50, 62, 74, 86, 98, 110, 111, 2}, |
|||
{27, 39, 51, 63, 75, 87, 99, 100, 112, 3, 15}, |
|||
{40, 52, 64, 76, 88, 89, 101, 113, 4, 16, 28}, |
|||
{53, 65, 77, 78, 90, 102, 114, 5, 17, 29, 41}, |
|||
{66, 67, 79, 91, 103, 115, 6, 18, 30, 42, 54} |
|||
Magic number is 671 |
|||
Output from code that checks the results |
|||
Rows |
|||
{671,671,671,671,671,671,671,671,671,671,671} |
|||
Columns |
|||
{671,671,671,671,671,671,671,671,671,671,671} |
|||
Diagonals |
|||
671 |
|||
671 |
|||
=={{header|Maxima}}== |
=={{header|Maxima}}== |