include "alldifferent.mzn"; int : n = 3; int : magic_number = n*(n*n + 1) div 2; array[1..n, 1..n] of var 1..n*n : carre; % All numbers must be different constraint alldifferent([carre[i,j] | i,j in 1..n]); % The sum of the numbers in the diagonal is equal to the magic number constraint sum([carre[i,i] | i in 1..n]) == magic_number; % The sum of the numbers in the anti-diagonal is equal to the magic number constraint sum([carre[i,n-i+1] | i in 1..n]) == magic_number; % The sum of the numbers of each line is equal to the magic number constraint forall([sum([carre[k,i] | i in 1..n]) == magic_number | k in 1..n]); % The sum of the numbers of each column is equal to the magic number constraint forall([sum([carre[i,k] | i in 1..n]) == magic_number | k in 1..n]); solve satisfy;