3 ms·
Yes, this is easy to add, since there is a dedicated constraint called all_distinct/1 that we can use to express that a set of integers be pairwise distinct. I
by zmonx 9y ago
Yes, this is easy to add, since there is a dedicated constraint called all_distinct/1 that we can use to express that a set of integers be pairwise distinct.
In this concrete case, we get this by simply adding all_distinct(Ds) to the query. For example:
?- solution(Ds),
all_distinct(Ds),
reverse(Ds, Rs),
label(Rs).
This yields Ds = [6, 5, 4, 8, 7, 3, 1, 2, 0], in addition to other solutions. Now we only need to add that none of the integers must be 0. We can express this (for example) with the additional constraint Ds ins 1..9:
?- solution(Ds),
Ds ins 1..9,
all_distinct(Ds),
reverse(Ds, Rs),
label(Rs).
However, when you run this query, you get false in return. This means that there is no solution that satisfies all these constraints at the same time.
Therefore, the task you mentioned was probably about the prefices, i.e., the initial segments of the number. We can easily express this with small modifications to the program above:
solution(Ds) :-
length(Ds, 9),
Ds ins 1..9,
all_distinct(Ds),
foldl(segment_divisible(Ds), Ds, 1, _).
segment_divisible(Ds, _, I0, I) :-
I #= I0 + 1,
length(Prefix, I0),
append(Prefix, _, Ds),
reverse(Prefix, RPs),
foldl(digits_number, RPs, 0-0, N-_),
N #= I0*_.
digits_number(D, I0-P0, I-P) :-
I #= I0 + D*10^P0,
P #= P0 + 1.
With this program, we get:
?- solution(Ds),
label(Ds).
Ds = [3, 8, 1, 6, 5, 4, 7, 2, 9] ;
false.
This means that there is a solution, and it is in fact unique! This is determined very efficiently thanks to the pruning that is automatically applied by the constraint solver.
- JadeNB 9y agoI'm sorry for the delay; I didn't realise that you'd responded. I couldn't find the O'Keefe book to look up the exact challenge, but I think you've correctly reverse-engineered and solved it. Thanks again!