% A riddle my mother asked me to solve a while back (09Oct23) :- autoload(library(apply), [maplist/2]). :- autoload(library(bounds), [label/1]). :- autoload(library(clp_distinct), [all_distinct/1]). :- autoload(library(solution_sequences), [distinct/1]). % Imagine a grid like this, where each field has a numerical value % associated with it: % A % B C D % E % The constraints are defined here: cross(N, Sol) :- Vs = [A, B, C, D, E], % all fields have at least a value of 1 maplist(#<(0), Vs), % all fiends have distinct values all_distinct(Vs), % all fiends add up to 15 A + B + C + D + E #= 15, % row and column add up to N A + C + E #= N, B + C + D #= N, % the middle field is the solution C = Sol, % force a concrete value to be instanciated. label(Vs). % The problem consists of determining what the value in the middle % field will be, depending on what the horizonal and vertical sum is % allowed to be: ?- N #> 3, distinct(cross(N, C)). % N = 9, C = 3 ; % N = 10, C = 5 ; % N = 8, C = 1 ;