Department numbers: Difference between revisions

Line 3,115:
Police: 6, Sanitation: 5, Fire: 1
14 valid combinations found.</pre>
 
=={{header|Picat}}==
 
===Constraint model===
<lang Picat>import cp.
 
go ?=>
N = 7,
Sols = findall([P,S,F], department_numbers(N, P,S,F)),
println(" P S F"),
foreach([P,S,F] in Sols)
printf("%2d %2d %2d\n",P,S,F)
end,
nl,
printf("Number of solutions: %d\n", Sols.len),
nl.
go => true.
 
department_numbers(N, Police,Sanitation,Fire) =>
Police :: 1..N,
Sanitation :: 1..N,
Fire :: 1..N,
all_different([Police,Sanitation,Fire]),
Police + Sanitation + Fire #= 12,
Police mod 2 #= 0,
solve([Police,Sanitation,Fire]).</lang>
 
{{out}}
<pre> P S F
2 3 7
2 4 6
2 6 4
2 7 3
4 1 7
4 2 6
4 3 5
4 5 3
4 6 2
4 7 1
6 1 5
6 2 4
6 4 2
6 5 1
 
Number of solutions: 14</pre>
 
===Loop===
<lang Picat>go2 => department_numbers2(N) =>
println(" P S F"),
foreach(P in 1..N, P mod 2 == 0)
foreach(S in 1..N, P != S)
foreach(F in 1..N, F != P, F != S, P + S + F == 12)
printf("%2d %2d %2d\n",P,S,F)
end
end
end.</lang>
 
===List comprehension===
<lang Picat>import util.
 
department_numbers3(N) =>
println("P S F"),
L = [[P.to_string,S.to_string,F.to_string] : P in 1..N, P mod 2 == 0,
S in 1..N, P != S,
F in 1..N,
F != P, F != S, P + S + F == 12],
println(map(L,join).join("\n")).</lang>
 
===Prolog style===
{{trans|Prolog}}
<lang Picat>go :-
println("P F S"),
assign(Police, Fire, Sanitation),
printf("%w %w %w\n", Police, Fire, Sanitation),
fail,
nl.
 
dept(X) :- between(1, 7, X).
 
police(X) :- member(X, [2, 4, 6]).
fire(X) :- dept(X).
san(X) :- dept(X).
assign(A, B, C) :-
police(A), fire(B), san(C),
A != B, A != C, B != C,
12 is A + B + C.</lang>
 
 
 
=={{header|PicoLisp}}==
495

edits