this repo has no description
1% magicsq.mzn.model
2% vim: ft=zinc ts=4 sw=4 et tw=0
3%
4% A model for magic square problems.
5
6include "globals.mzn";
7
8int: n; % The length of side of the square.
9
10int: s = (n * (n * n + 1)) div 2;
11 % The Magic Constant - see
12 % http://mathworld.wolfram.com/MagicSquare.html
13
14array [1..n, 1..n] of var 1..(n * n): a;
15
16% Every square must have a different value.
17
18constraint all_different (r, c in 1..n) (a[r, c]);
19
20% Every row, column, and major diagonal must sum to the Magic Constant.
21
22constraint forall (c in 1..n) (sum (r in 1..n) (a[r, c]) = s);
23constraint forall (r in 1..n) (sum (c in 1..n) (a[r, c]) = s);
24constraint sum (i in 1..n) (a[i, i]) = s;
25constraint sum (i in 1..n) (a[i, n + 1 - i]) = s;
26
27% Any solution will do.
28
29solve satisfy;
30
31output [
32 show_int(floor(log10(int2float(n * n))) + 1, a[r, c]) ++
33 if c = n then "\n" else " " endif
34 | r, c in 1..n
35];