Prolog - matrix equation

I'm new to Prolog and I want to do something like this, but I don't know where to start. Already done the matrix and how to check that the numbers are under 9 with the Bounds library, but about that, wrestle with this for several days.

It should look like this:

2+7-4=5
+ - *
9-5*2=8
- + -
4*3-8=4
= = =
7 5 0

      

The basic idea is to provide Prolog with a semi-filled matrix so that it can complete it. Thanks for any information or ideas you can give me in advance.

Code for solving 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.

      

+3


source to share


1 answer


Use , use library (clpfd)) , not library (boundaries) !

:- use_module(library(clpfd)).

      

We can specify constraints to be met like this:

m3x3_zs (Mss, Zs): -
   Mss = [[M11, M12, M13], [M21, M22, M23], [M31, M32, M33]],
   Zs = [M11, M12, M13, M21, M22, M23, M31, M32, M33],

   Zs ins 0..9,

   5 # = M11 + M12-M13,% row constraints
   8 # = (M21-M22) * M23 ,% (see text below for details)
   4 # = M31 * M32-M33,

   7 # = M11 + M21-M31,% column constraints
   5 # = M12-M22 + M23,
   0 # = M13 * M23-M33.

Note the highlighted target 8 #= <b>(M21-M22)*M23</b>

above! If we used the general precedence rules for A-B*C

, we would have 8 #= M21-(M22*M23)

, but that would rule out the exemplary solution [[2,7,4],[9,5,2],[4,3,8]]

you gave in the OP.



Now you can search for all solutions using an enumeration predicate labeling/2

!

? - m3x3_zs (Mss, Zs), labeling ([], Zs).
  Mss = [[0,5,0], [9,1,1], [2,2,0]], Zs = [0,5,0,9,1,1,2,2,0]
; Mss = [[0,5,0], [9,8,8], [2,2,0]], Zs = [0,5,0,9,8,8,2,2,0]
; Mss = [[0,7,2], [8,4,2], [1,8,4]], Zs = [0,7,2,8,4,2,1,8,4]
; Mss = [[0,8,3], [9,5,2], [2,5,6]], Zs = [0,8,3,9,5,2,2,5,6]
; Mss = [[1,4,0], [8,0,1], [2,2,0]], Zs = [1,4,0,8,0,1,2,2,0]
; Mss = [[1,4,0], [8,7,8], [2,2,0]], Zs = [1,4,0,8,7,8,2,2,0]
; Mss = [[1,5,1], [9,8,8], [3,4,8]], Zs = [1,5,1,9,8,8,3,4,8]
; Mss = [[1,6,2], [7,3,2], [1,8,4]], Zs = [1,6,2,7,3,2,1,8,4]
; Mss = [[1,7,3], [8,4,2], [2,5,6]], Zs = [1,7,3,8,4,2,2,5,6]
; Mss = [[1,8,4], [9,5,2], [3,4,8]], Zs = [1,8,4,9,5,2,3,4,8]
; Mss = [[2,3,0], [7,6,8], [2,2,0]], Zs = [2,3,0,7,6,8,2,2,0]
; Mss = [[2,4,1], [8,7,8], [3,4,8]], Zs = [2,4,1,8,7,8,3,4,8]
; Mss = [[2,5,2], [6,2,2], [1,8,4]], Zs = [2,5,2,6,2,2,1,8,4]
; Mss = [[2,6,3], [7,3,2], [2,5,6]], Zs = [2,6,3,7,3,2,2,5,6]
; Mss = [[2,7,4], [8,4,2], [3,4,8]], Zs = [2,7,4,8,4,2,3,4,8]
; Mss = [[3,2,0], [6,5,8], [2,2,0]], Zs = [3,2,0,6,5,8,2,2,0]
; Mss = [[3,3,1], [7,6,8], [3,4,8]], Zs = [3,3,1,7,6,8,3,4,8]
; Mss = [[3,4,2], [5,1,2], [1,8,4]], Zs = [3,4,2,5,1,2,1,8,4]
; Mss = [[3,5,3], [6,2,2], [2,5,6]], Zs = [3,5,3,6,2,2,2,5,6]
; Mss = [[3,6,4], [7,3,2], [3,4,8]], Zs = [3,6,4,7,3,2,3,4,8]
; Mss = [[4,1,0], [5,4,8], [2,2,0]], Zs = [4,1,0,5,4,8,2,2,0]
; Mss = [[4,2,1], [6,5,8], [3,4,8]], Zs = [4,2,1,6,5,8,3,4,8]
; Mss = [[4,3,2], [4,0,2], [1,8,4]], Zs = [4,3,2,4,0,2,1,8,4]
; Mss = [[4,4,3], [5,1,2], [2,5,6]], Zs = [4,4,3,5,1,2,2,5,6]
; Mss = [[4,5,4], [6,2,2], [3,4,8]], Zs = [4,5,4,6,2,2,3,4,8]
; Mss = [[5,0,0], [4,3,8], [2,2,0]], Zs = [5,0,0,4,3,8,2,2,0]
; Mss = [[5,1,1], [5,4,8], [3,4,8]], Zs = [5,1,1,5,4,8,3,4,8]
; Mss = [[5,3,3], [4,0,2], [2,5,6]], Zs = [5,3,3,4,0,2,2,5,6]
; Mss = [[5,4,4], [5,1,2], [3,4,8]], Zs = [5,4,4,5,1,2,3,4,8]
; Mss = [[6,0,1], [4,3,8], [3,4,8]], Zs = [6,0,1,4,3,8,3,4,8]
; Mss = [[6,3,4], [4,0,2], [3,4,8]], Zs = [6,3,4,4,0,2,3,4,8]
; false.

How about a little more specific? What solutions do they have 4

in the middle?

? - m3x3_zs (Mss, Zs), Mss = [_, [_, 4 , _], _], labeling ([], Zs).
  Mss = [[0,7,2], [8, 4 , 2], [1,8,4]], Zs = [0,7,2,8, 4 , 2,1,8,4]
; Mss = [[1,7,3], [8, 4 , 2], [2,5,6]], Zs = [1,7,3,8, 4 , 2,2,5,6]
; Mss = [[2,7,4], [8, 4 , 2], [3,4,8]], Zs = [2,7,4,8, 4 , 2,3,4,8]
; Mss = [[4,1,0], [5, 4 , 8], [2,2,0]], Zs = [4,1,0,5, 4 , 8,2,2,0]
; Mss = [[5,1,1], [5, 4 , 8], [3,4,8]], Zs = [5,1,1,5, 4 , 8,3,4,8].
+2


source







All Articles