The Send More Money problem consists of finding unique digits for the letters D, E, M, N, O, R, S, and Y such that S and M are different from zero (no leading zeros) and the following equation is satisfied:
S E N D |
+ M O R E |
M O N E Y |
You can use the CLP solver to formulate this problem as a CSP by using an integer variable to represent each letter in the expression. The comments before each statement in the following code introduce variables, domains, and constraints:
/* Send More Money */
proc optmodel; /* Declare all variables as integer. */ var S integer, E integer, N integer, D integer, M integer, O integer, R integer, Y integer; /* Set all domains to between 0 and 9. Domains are unbounded by default. Always declare domains to be as tight as possible. */ for {j in 1.._NVAR_} do; _VAR_[j].lb = 0; _VAR_[j].ub = 9; end; /* Describe the arithmetic constraint.*/ con Arithmetic: 1000*S + 100*E + 10*N + D + 1000*M + 100*O + 10*R + E = 10000*M + 1000*O + 100*N + 10*E + Y; /* Forbid leading letters from taking the value zero. Constraint names are optional. */ con S ne 0; con M ne 0; /* Require all variables to take distinct values. */ con alldiff(S E N D M O R Y); solve; print S E N D M O R Y; quit;
The domain of each variable is the set of digits 0 through 9. The VAR statement identifies the variables in the problem. The Arithmetic constraint defines the linear constraint SEND + MORE = MONEY and the restrictions that S and M cannot take the value 0. (Alternatively, you can simply specify the domain for S and M as in the VAR statement.) Finally, the ALLDIFF predicate enforces the condition that the assignment of digits should be unique.
The PRINT statement produces the solution output as shown in Figure 6.1.
Figure 6.1: Solution to SEND + MORE = MONEY
Problem Summary | |
---|---|
Objective Sense | Minimization |
Objective Function | (0) |
Objective Type | Constant |
Number of Variables | 8 |
Bounded Above | 0 |
Bounded Below | 0 |
Bounded Below and Above | 8 |
Free | 0 |
Fixed | 0 |
Binary | 0 |
Integer | 8 |
Number of Constraints | 4 |
Linear LE (<=) | 0 |
Linear EQ (=) | 1 |
Linear GE (>=) | 0 |
Linear LT (<) | 0 |
Linear NE (~=) | 2 |
Linear GT (>) | 0 |
Linear Range | 0 |
Alldiff | 1 |
Element | 0 |
GCC | 0 |
Lexico (<=) | 0 |
Lexico (<) | 0 |
Pack | 0 |
Reify | 0 |
Constraint Coefficients | 10 |
The CLP solver determines the following unique solution to this problem:
9 5 6 7 |
+ 1 0 8 5 |
1 0 6 5 2 |