Cryptarithmetic puzzle Prolog

878 views Asked by At

I have the following task in Prolog:

Find an assignment of the letters to digits (each letter to a different digit) so that the following product holds:

EAU * EAU = OCEAN

If the code for this is as follows:

:- use_module(library(clpfd)).

ocean(L) :-
   L = [O,C,E,A,N],
   L2 = [U|L],
   all_different(L2),
   L2 ins 0..9,
   EAU #= E*100 + A*10 + U,
   OCEAN #= O*10000 + C * 1000 + E*100 + A*10 + N,
   EAU * EAU #= OCEAN,
   labeling([], L2).

Can someone please explain the following:

What is the meaning of L2 ins 0..9 (Why is the range between these numbers?)

What is the meaning of multiplying the letters by these particular numbers *10000 + C * 1000 + E*100, and E*100 + A*10 + U etc.?

Many thanks in advance for any guidance provided.

1

There are 1 answers

0
Sergii Dymchenko On BEST ANSWER

'0..9' just means a single digit: 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

EAU #= E*100 + A*10 + U is for indication that a number 'EAU' consist of digits 'E', 'A' and 'U' in that particular order. For example, if you have a number 735 = 7*100 + 3*10 + 5.

Similarly for 'OCEAN', a five-digit number.

Also, constraints in the program you provided look incomplete to me. It should also be stated that the first digit of every number is greater than 0, because usually we don't use numbers with leading zeros like '0123'.