Toggle navigation
?
users online
Logout
Open hangout
Open chat for current file
% Ensure a number is bound ensure_number(N, Numbers, Numbers):- nonvar(N). ensure_number(N, NumbersIn, NumbersOut):- var(N), select(N, NumbersIn, NumbersOut). % Ensure all number are bound ensure_all_numbers([], Numbers, Numbers). ensure_all_numbers([H|T], NumbersIn, NumbersOut):- ensure_number(H, NumbersIn, N2), ensure_all_numbers(T, N2, NumbersOut). % Check V1 + V2 = R check_add(V1, V2, R, NumbersIn, NumbersOut):- ensure_number(V1, NumbersIn, N2), ensure_number(V2, N2, NumbersOut), S is V1 + V2, S = R. % Check abs(X-Y) = R check_abs_diff(X, Y, R, NumbersIn, NumbersOut):- ensure_number(X, NumbersIn, N2), ensure_number(Y, N2, NumbersOut), D is abs(X - Y), D = R. % Check one of the allowed number check_number(X, Allowed, NumbersIn, NumbersOut):- ensure_number(X, NumbersIn, NumbersOut), member(X, Allowed). % start solving! run(Sol):- Sol = [A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P], N1=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16], check_number(A, [2,4,6,8], N1, N2), check_number(B, [2,4,6,8], N2, N3), check_number(J, [2,4,6,8], N3, N4), check_number(K, [2,4,6,8], N4, N5), check_add(B, K, 14, N5, N6), check_add(E, F, 26, N6, N7), check_add(K, O, 9, N7, N8), check_add(A, L, 9, N8, N9), check_add(C, I, 25, N9, N10), check_add(A, G, 13, N10, N11), check_add(C, P, 24, N11, N12), check_add(B, M, 9, N12, N13), check_add(A, K, 12, N13, N14), check_add(D, E, 25, N14, N15), check_abs_diff(E, N, 4, N15, N16), ensure_number(H, N16, []) . /** <examples> ?- run(Sol) */