Debian Codegraph @

tests/spec/examples/magicsq.mzn.model

raw
1 % magicsq.mzn.model
2 % vim: ft=zinc ts=4 sw=4 et tw=0
3 %
4 % A model for magic square problems.
5
6 include "globals.mzn";
7
8 int: n; % The length of side of the square.
9
10 int: s = (n * (n * n + 1)) div 2;
11 % The Magic Constant - see
12 % http://mathworld.wolfram.com/MagicSquare.html
13
14 array [1..n, 1..n] of var 1..(n * n): a;
15
16 % Every square must have a different value.
17
18 constraint 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
22 constraint forall (c in 1..n) (sum (r in 1..n) (a[r, c]) = s);
23 constraint forall (r in 1..n) (sum (c in 1..n) (a[r, c]) = s);
24 constraint sum (i in 1..n) (a[i, i]) = s;
25 constraint sum (i in 1..n) (a[i, n + 1 - i]) = s;
26
27 % Any solution will do.
28
29 solve satisfy;
30
31 output [
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 ];