Information Technology Reference
In-Depth Information
Figure 5.6.
Forced values in Sudoku
There is only one possible location for a
3
on the
second row: it must be located at position
3
.
The other three locations are ruled out by the
3
at
position
(
2, 3
)
(
1, 1
)
and the one at position
(
4, 4
)
.
3
The
3
on the third row is similar.
5.3 A second example: Cryptarithmetic
Cryptarithmetic problems are of the following form. Given is an equation using letters
like this:
SEND
+ MORE
MONEY
A distinct digit must be found for each letter such that the equation holds. (Also, the
leading leftmost digits,
S
and
M
, must not be 0.) A solution for this example is
S
=
9,
E
=
5,
N
=
6,
D
=
7,
M
=
1,
O
=
0,
R
=
8,
Y
=
2
since 9567
10652. As a preliminary to solving problems like this in Prolog,
the next section examines the facilities Prolog provides for arithmetic.
+
=
1085
5.3.1 Arithmetic in Prolog
Numbers in Prolog were first mentioned in the review in figure 3.10, which said that a
Prolog term is either a constant, variable, or number, and that a
number
is a sequence
of one or more digits optionally preceded by a minus sign and optionally containing
a decimal point (for rational numbers).
Since numbers are terms, they can appear in programs and queries anywhere a
constant or variable can appear. So Prolog program can have clauses like
age(donna,23).
age(andy,22).
current_temperature(-5.2).
and queries like the following: