:- use_module(library(clpfd)). strictly_ascending([]). strictly_ascending([S|Ss]) :- strictly_ascending_(Ss, S), strictly_ascending(Ss). strictly_ascending_([], _). strictly_ascending_([A|_], S) :- A #> S. %?- strictly_ascending(As), As ins 0..5, label(As). mstd(Set) :- findall(S, (member(A, Set), member(B, Set), S is A + B), Sums), findall(D, (member(A, Set), member(B, Set), D is A - B), Diffs), sort(Sums, SSums), sort(Diffs, SDiffs), length(SSums, LS), length(SDiffs, LD), LS > LD. sumdiff(N, Ls) :- length(Ls, N), between(0, inf, Limit), Ls ins 0..Limit, strictly_ascending(Ls), label(Ls), mstd(Ls), !. %?- mstd([0,2,3,4,7,11,12,14]). %?- Sums = [2, 3, 4, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 21, 23, 25, 26], length(Sums, L). %?- Diffs = [-14, -12, -11, -10, -9, -8, -7, -5, -4, -3, -2, -1, 1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 12, 14], length(Diffs, L). %?- sumdiff(8, L). %?- mstd([0,1,2]). %?- mstd([0,14]).