I'm new to Prolog and I want to do something like this, but don't know where to start. Already made the matrix and how to verify the numbers are under 9 with Bounds library, but that's about it, have been struggling with this for days.
It should look like this:
2+7-4=5
+ - *
9-5*2=8
- + -
4*3-8=4
= = =
7 5 0
The main idea is to give Prolog a semi-filled matrix so he can complete it. Thanks for any info or ideas you can give me in advance.
A code for resolving equations by placing operators with given numbers:
:- use_module(library(bounds)).
lista([X]) --> [X].
lista([H|T]) --> [H], op, lista(T).
op --> [+].
op --> [-].
op --> [*].
op --> [/].
op --> [''].
puzzle(Num, Res) :-
permutation(Num, Numbperm),
lista(Numbperm, Lista, []),
concat_atom([Res, =:=|Lista], At),
term_to_atom(Ev, At),
call(Ev),
write(Ev), nl.
To utilize clpfd, let's use library(clpfd)), not library(bounds)!
We can state the constraints that must hold like this:
Note the highlighted goal
8 #= <b>(M21-M22)*M23</b>
above! If we had used the common precedence rules forA-B*C
we would have had8 #= M21-(M22*M23)
, but that would have excluded the sample solution[[2,7,4],[9,5,2],[4,3,8]]
you gave in the OP.Now let's search all solutions by using the enumeration predicate
labeling/2
!How about a little more specific? Which solutions have
4
in the middle?