Varun Chopra
Varun Chopra

Reputation: 43

Error in Prolog Code(operator Expected)

I am new to Prolog programming and trying to implement 3 by 3 magic square in prolog. But I am getting an operator expected error while compiling the program, please tell me what is wrong in that statement. Thanks

Code:

  :- use_module(library(clpfd),[]).



    magic_square(Puzzle, Solution, Sum) :-
      Puzzle = [S11, S12, S13,
                S21, S22, S23,
                S31, S32, S33],


        Puzzle ins 1..9,  %Statement where it is saying operator expected 
        all_different(Puzzle),




           label(Puzzle),

      /* Rows */
      R1 = [S11, S12, S13],
      R2 = [S21, S22, S23],
      R3 = [S31, S32, S33],

      /* Columns */
      C1 = [S11, S21, S31],
      C2 = [S12, S22, S32],
      C3 = [S13, S23, S33],

      /* Diagonals */
      Diag1 = [S11, S22, S33],
      Diag2 = [S13, S22, S31],

      sum_list(R1, Sum1),
      sum_list(R2, Sum2),
      sum_list(R3, Sum3),
      sum_list(C1, Sum4),
      sum_list(C2, Sum5),
      sum_list(C3, Sum6),
      sum_list(Diag1, Sum7),
      sum_list(Diag2, Sum8),

      Sum1 = Sum2,
      Sum2 = Sum3,
      Sum3 = Sum4,
      Sum4 = Sum5,
      Sum5 = Sum6,
      Sum6 = Sum7,
      Sum7 = Sum8,

      Sum = Sum8,
      Solution = Puzzle.

Upvotes: 1

Views: 699

Answers (1)

repeat
repeat

Reputation: 18726

Instead of

:- use_module(library(clpfd),[]).

you should use

:- use_module(library(clpfd)).

Like so:

:- use_module(library(clpfd)).

magicSquare3_sum(Zs,Sum) :-
   Zs = [S11,S12,S13,
         S21,S22,S23,
         S31,S32,S33],

   Zs ins 1..9, 
   all_different(Zs),

   S11 + S12 + S13 #= Sum,     % rows
   S21 + S22 + S23 #= Sum,
   S31 + S32 + S33 #= Sum,

   S11 + S21 + S31 #= Sum,     % columns
   S12 + S22 + S32 #= Sum,
   S13 + S23 + S33 #= Sum,

   S11 + S22 + S33 #= Sum,     % diagonals
   S13 + S22 + S31 #= Sum.

Sample query:

?- magicSquare3_sum(Zs,Sum), labeling([],Zs).
Zs = [2, 7, 6, 9, 5, 1, 4, 3, 8], Sum = 15 ;
Zs = [2, 9, 4, 7, 5, 3, 6, 1, 8], Sum = 15 ;
Zs = [4, 3, 8, 9, 5, 1, 2, 7, 6], Sum = 15 ;
Zs = [4, 9, 2, 3, 5, 7, 8, 1, 6], Sum = 15 ;
Zs = [6, 1, 8, 7, 5, 3, 2, 9, 4], Sum = 15 ;
Zs = [6, 7, 2, 1, 5, 9, 8, 3, 4], Sum = 15 ;
Zs = [8, 1, 6, 3, 5, 7, 4, 9, 2], Sum = 15 ;
Zs = [8, 3, 4, 1, 5, 9, 6, 7, 2], Sum = 15 ;
false.

Upvotes: 2

Related Questions