How to resolve the algorithm Department numbers step by step in the Picat programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Department numbers step by step in the Picat programming language
Table of Contents
Problem Statement
There is a highly organized city that has decided to assign a number to each of their departments:
Each department can have a number between 1 and 7 (inclusive). The three department numbers are to be unique (different from each other) and must add up to 12. The Chief of the Police doesn't like odd numbers and wants to have an even number for his department.
Write a computer program which outputs all valid combinations.
Possible output (for the 1st and 14th solutions):
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Department numbers step by step in the Picat programming language
Source code in the picat programming language
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]).
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.
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")).
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.
You may also check:How to resolve the algorithm Function definition step by step in the ALGOL W programming language
You may also check:How to resolve the algorithm Loops/For with a specified step step by step in the Pascal programming language
You may also check:How to resolve the algorithm Commatizing numbers step by step in the Delphi programming language
You may also check:How to resolve the algorithm Probabilistic choice step by step in the Stata programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the ATS programming language