您可以尝试使用 permutation/2,也许它比您的更快(我不确定,应该对其进行基准测试)。无论如何,排列通常需要不同的方法,例如 CLP(FD)。我复制了您的代码,并对其进行了修改:
:- use_module(library(clpfd)).
diabolico([A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P]) :-
Vs = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P],
Vs ins 1..16,
all_different(Vs),
A+B+C+D#=34,
E+F+G+H#=34,
I+J+K+L#=34,
M+N+O+P#=34,
A+E+I+M#=34,
B+F+J+N#=34,
C+G+K+O#=34,
D+H+L+P#=34,
M+B+G+L#=34,
I+N+C+H#=34,
E+J+O+D#=34,
A+F+K+P#=34,
P+C+F+I#=34,
L+O+B+E#=34,
H+K+N+A#=34,
D+G+J+M#=34,
label(Vs).
writerows([]).
writerows([A,B,C,D|Rs]) :-
format('~|~t~d~3+~|~t~d~3+~|~t~d~3+~|~t~d~3+~n', [A,B,C,D]),
writerows(Rs).
这是一个示例:
?- diabolico(X), writerows(X).
1 8 10 15
12 13 3 6
7 2 16 9
14 11 5 4
X = [1, 8, 10, 15, 12, 13, 3, 6, 7|...] ;
1 8 10 15
14 11 5 4
7 2 16 9
12 13 3 6
X = [1, 8, 10, 15, 14, 11, 5, 4, 7|...] ;
1 8 11 14
12 13 2 7
6 3 16 9
15 10 5 4
X = [1, 8, 11, 14, 12, 13, 2, 7, 6|...]
...
我对您的 permutar/2 实施表示赞赏:它比 permutation/2 好得多:
?- X=[1,2,3,4,5,6,7,8], time(aggregate(count,X^Y^permutation(X,Y),C)).
% 328,837 inferences, 0.171 CPU in 0.172 seconds (99% CPU, 1927406 Lips)
X = [1, 2, 3, 4, 5, 6, 7, 8],
C = 40320.
?- X=[1,2,3,4,5,6,7,8], time(aggregate(count,X^Y^permutar(X,Y),C)).
% 86,597 inferences, 0.079 CPU in 0.081 seconds (99% CPU, 1091190 Lips)
X = [1, 2, 3, 4, 5, 6, 7, 8],
C = 40320.
唉,那么我最初的建议完全没用......