Mathematica programming

Friday, July 22, 2005

Magic Squares


Magic square is a square of integer numbers such that sum
of numbers in each row, each column and two diagonals
are the same.


Generating magic squares is easy with Mathematica. For instance,
solving for 4 by 4 matrices

vars = Variables[matrix = Array[a, {4, 4}]];

eqs = Equal @@ Join[
Plus @@@ matrix, (* sum of rows *)
Plus @@@ Transpose[matrix], (* of columns *)
{Tr[matrix ], Tr[Reverse /@ matrix] } (* of diagonals *)
];

sol =
matrix /. ToRules[And @@
Cases[Reduce[eqs , vars, Integers], _Equal]]

we get an answer in terms of free 8 integers

{{n1, n2, n3, n4},
{n5, n6, n7, n1 + n2 + n3 + n4 - n5 - n6 - n7},
{n8, n1 - n4 + n5 - n7 + n8,
n2 + n3 + 2 n4 - n5 - n6 - n8, n6 + n7 - n8},
{n2 + n3 + n4 - n5 - n8, n3 + 2 n4 - n5 - n6 +
n7 - n8, n1 - n3 - n4 + n5 + n6 - n7 + n8,
-n4 + n5 + n8}
}

Monday, July 18, 2005

Opening...


This blog's goal is educational and recreational, and revolves around programming in Mathematica, famous computer algebra system. Mathematica, besides being a program for doing calculations, is also a programming language, which is sufficiently powerful to be worth learning especially if you do many calculations in Mathematica.


The original goal would be about one post a week, but we'll see about that. I have to mention that I am Wolfram Research employee, and that this blog is my own initiative. The source of inspiration for topics discussed will be taken from question posted at MathGroup and will aim at students and researchers working with Mathematica. You are welcome to suggest a topic using comments, or writing to the address being output of

StringJoin[Insert[Insert[FromCharacterCode /@ (Prime[25]+
FromDigits/@ Partition[ IntegerDigits[
Times @@ {4, 7, 89, 45119, 2179787, 1573939859, 3111290531015993}
], 2] ), ".", 4{{2},{-1}}], "@", -10]]