COIN-OR::LEMON - Graph Library

source: lemon-project-template-glpk/deps/glpk/examples/magic.mod @ 10:5545663ca997

subpack-glpk
Last change on this file since 10:5545663ca997 was 9:33de93886c88, checked in by Alpar Juttner <alpar@…>, 13 years ago

Import GLPK 4.47

File size: 1.5 KB
Line 
1/* MAGIC, Magic Square */
2
3/* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
4
5/* In recreational mathematics, a magic square of order n is an
6   arrangement of n^2 numbers, usually distinct integers, in a square,
7   such that n numbers in all rows, all columns, and both diagonals sum
8   to the same constant. A normal magic square contains the integers
9   from 1 to n^2.
10
11   (From Wikipedia, the free encyclopedia.) */
12
13param n, integer, > 0, default 4;
14/* square order */
15
16set N := 1..n^2;
17/* integers to be placed */
18
19var x{i in 1..n, j in 1..n, k in N}, binary;
20/* x[i,j,k] = 1 means that cell (i,j) contains integer k */
21
22s.t. a{i in 1..n, j in 1..n}: sum{k in N} x[i,j,k] = 1;
23/* each cell must be assigned exactly one integer */
24
25s.t. b{k in N}: sum{i in 1..n, j in 1..n} x[i,j,k] = 1;
26/* each integer must be assigned exactly to one cell */
27
28var s;
29/* the magic sum */
30
31s.t. r{i in 1..n}: sum{j in 1..n, k in N} k * x[i,j,k] = s;
32/* the sum in each row must be the magic sum */
33
34s.t. c{j in 1..n}: sum{i in 1..n, k in N} k * x[i,j,k] = s;
35/* the sum in each column must be the magic sum */
36
37s.t. d: sum{i in 1..n, k in N} k * x[i,i,k] = s;
38/* the sum in the diagonal must be the magic sum */
39
40s.t. e: sum{i in 1..n, k in N} k * x[i,n-i+1,k] = s;
41/* the sum in the co-diagonal must be the magic sum */
42
43solve;
44
45printf "\n";
46printf "Magic sum is %d\n", s;
47printf "\n";
48for{i in 1..n}
49{  printf{j in 1..n} "%3d", sum{k in N} k * x[i,j,k];
50   printf "\n";
51}
52printf "\n";
53
54end;
Note: See TracBrowser for help on using the repository browser.