Home > Archive > Prolog > December 2006 > Generate all 384 diabolic square
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Generate all 384 diabolic square
|
|
|
|
I'm tyring to generate all the possible 384 diabolic squares (4x4
matrix) using the rule below, but I'm not having any luck, Can anybody
please advise?? I think it's failing when a particular permutation is
not a magic square, how do it change the rule so once it knows that the
list is not a magic square, generate another permutation???
thanks in advance..
:- dynamic my_func/2.
my_func([X1,X2,X3,X4,X5,X6,X7,X8,X9,X10,
X11,X12,X13,X14,X15,X16], X) :-
X1+X2+X3+X4 =:= 34,
X5+X6+X7+X8 =:= 34,
X9+X10+X11+X12 =:= 34,
X13+X14+X15+X16 =:= 34,
X1+X5+X9+X13 =:= 34,
X2+X6+X10+X14 =:= 34,
X3+X7+X11+X15 =:= 34,
X4+X8+X12+X16 =:=34,
X1+X6+X11+X16 =:=34,
X4+X7+X10+X13 =:=34,
write(X1), write(' '), write(X2), write(' '), write(X3), write('
'), write(X4), nl,
write(X5), write(' '), write(X6), write(' '), write(X7), write('
'), write(X8), nl,
write(X9), write(' '), write(X10), write(' '), write(X11),
write(' '), write(X12), nl,
write(X13), write(' '), write(X14), write(' '), write(X15),
write(' '), write(X16)
assert(my_func).
gen_all :-
permutation([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
X),
my_func(X, Y),
!.
| |
| virkony@gmail.com 2006-12-06, 8:02 am |
| Can't understand using of dynamic+assert in your implementation.
If you want to do caching of solution better way (I think) is to do:
:- dynamic cached_is_magic/1.
is_magic(L):-
cached_is_magic(L), !
;
L = [X1,...,X16],
X1+X2+X3+X4 is 34,
...,
assert(cached_is_magic(L)).
all_solutions(S):-
findall(L, (permutation(L), is_magic(L)), S).
|
|
|
|
|