Define a predicate magic_square(Square,Dim)
which unifies if its first
argument is a normal magic square with an order equal to the second argument.
A magic square is a square matrix of numbers. Each of the rows, columns and diagonals of the square have the same sum. This sum is called the magic constant. Every row and column in a magic square is unique. Every number occurs only one. If the order of the square is n, there are n rows and n columns. Each number from 1 to n² occurs once in a magic square of order n.
Below, we see the output of a predicate main/1, that writes magic squares of a given order to standard output followed by the magic constant.
?- main(3).
[2, 7, 6].
[9, 5, 1].
[4, 3, 8].
15
true ;
[2, 9, 4].
[7, 5, 3].
[6, 1, 8].
15
true ;