# Encoding magic_square.enc.asp

Name magic_square.enc.asp [Root]    Puzzles (51)    Grid (17) Barbara Nardi Yisong Wang and Jia-Huai You 2012-04-11 15:58 2012-04-12 11:37 No assSum(S) :- size(N), Sq = N*N, S = Sq+1. sum(S) :- assSum(As), size(N), X = N*As, S = X/2. data(1). data(D) :- data(D1), D = D1+1, size(N), Sq = N*N, D <= Sq. num(X) :- data(X), size(N), X <= N. sqr(I,J,A) v not_sqr(I,J,A) :- num(I), num(J), data(A). :- sqr(I,J,A), sqr(I,J,B), A != B. :- #count {I : sqr(I,J,A)} = 0 , data(A). %%%%%%%%%%%%%%%%%%%%%%%% semi magic % rows :- type(semi), num(I), sum(S), not #sum { A: sqr(I,J,A) } = S. % columns :- type(semi), num(J), sum(S), not #sum { A: sqr(I,J,A) } = S. %%%%%%%%%%%%%%%%%%%%%%%% normal magic % main diagonal :- type(normal), sum(S), not #sum { A: sqr(I,I,A) } = S. % secondary diagonal :- type(normal), sum(S), not #sum { A: num(I), size(N), D = N-I, J = D+1, sqr(I,J,A) } = S. %%%%%%%%%%%%%%%%%%%%%%%% pan magic % pan diagonal up :- sum(S), panDiaUp(_,_,O), not #sum { A: sqr(R,C,A), panDiaUp(R,C,O) } = S. % pan diagonal down :- sum(S), panDiaDown(_,_,O), not #sum { A: sqr(R,C,A), panDiaDown(R,C,O) } = S. panDiaUp(X,2,1) :- type(pan), size(X). panDiaUp(X,Y,O) :- panDiaUp(X,Z,Op), O = Op+1, Y = Z+1, Y <= X, size(X). panDiaUp(X,Y,O) :- panDiaUp(Z,W,O), X = Z-1, X > 0, next(W,Y). panDiaDown(1,2,1) :- type(pan). panDiaDown(1,Y,O) :- panDiaDown(1,Z,Op), O = Op+1, Y = Z+1, Y <= X, size(X). panDiaDown(X,Y,O) :- panDiaDown(Z,W,O), next(Z,X), next(W,Y). next(X,Y) :- num(X), Y = X+1, size(N), Y <= N. next(X,1) :- size(X). %%%%%%%%%%%%%%%%%%%%%%%% associative magic even :- type(assoc), size(N), num(X), N = X*2. odd :- type(assoc), not even. center(C) :- type(assoc), size(N), T = N+1, C = T/2. % odd case associatedOdd(X,Y) :- odd, center(C), X = C-1, Y= C+1. associatedOdd(X,Y) :- odd, associatedOdd(X1,Y1), X = X1-1, Y = Y1+1, X > 0, size(N), Y <= N. % row :- assSum(S), center(C), associatedOdd(X,Y), sqr(C,X,N1), sqr(C,Y,N2), T = N1 + N2, S != T. % column :- assSum(S), center(C), associatedOdd(X,Y), sqr(X,C,N1), sqr(Y,C,N2), T = N1 + N2, S != T. % main diagonal :- assSum(S), associatedOdd(X,Y), sqr(X,X,N1), sqr(Y,Y,N2), T = N1 + N2, S != T. % secondary diagonal :- assSum(S), associatedOdd(X,Y), sqr(X,Y,N1), sqr(Y,X,N2), T = N1 + N2, S != T. % even case associatedEven(C,Y) :- even, center(C), Y= C+1. associatedEven(X,Y) :- even, associatedEven(X1,Y1), X = X1-1, Y = Y1+1, X > 0, size(N), Y <= N. % main diagonal :- assSum(S), associatedEven(X,Y), sqr(X,X,N1), sqr(Y,Y,N2), T = N1 + N2, S != T. % secondary diagonal :- assSum(S), associatedEven(X,Y), sqr(X,Y,N1), sqr(Y,X,N2), T = N1 + N2, S != T.