Got it! This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
An alphametic puzzle (as referred to by the American Cryptogram Association) or cryptarithm (as referred to by the Journal of Recreational Mathematics) is a puzzle whose most common form involves assigning digits to letters such that the specified arithmetic constraints are satisfied.
For more information, or for more puzzles, an Internet search can be done for the words "alphametic puzzle" or "cryptarithm".
One of the most well-known alphametic puzzles is the following where no digit from 0 to 9 is used more than once and the zero digit, 0, may not be the first digit in a number.
S E N D
+ M O R E
---------
M O N E Y
Note: In this puzzle, S and M may not be 0, and only eight (8) of the possible ten ( 10) digits are used.
3. Permutation DSL
The permutation DSL is used to get the starting point for a Prolog program.
dsl permute
for "pro"
domain v
values 0 1 2 3 4 5 6 7 8 9
names S E N D M O R Y
end
Note: 10! is a large number, so the constraints have been added to reduce the amount of output.
Here is the Prolog code.
The solution can take a minute or so to complete.
Here is the output of the Prolog code.
4. Brute force
One way to approach this puzzle, in Prolog, is the brute force approach.
That is, generate each possibility and test that possibility.
A better way (fewer tests) is to add constraints as soon as possible to reduce the number of checks that must be made.
5. With carry
One way to do this is to use the grade-school method of adding using a carry.
C4 C3 C2 C1
S E N D
+ M O R E
-------------
M O N E Y
Here, C1, C2, C3, C4 are the carry for each operation and can only have a value of 0 or 1.
Let us assume that we are not smart enough to reason in a way such that we conclude that M must be 1 since it cannot be 0 and a carry, such as C4, can only have the value of 0 or 1, etc.
In part, that is the point of having a specification notation for problems such as this, so that one can use the computer to solve a problem when one is not smart enough, or so inclined, to solve it otherwise.
6. Permutation DSL
Once the order of the variables has been established, the permutation DSL can be used to generate the values in the order needed.
dsl permute
for "pro"
domain v
values 0 1 2 3 4 5 6 7 8 9
names E D Y R N O M S
end
Here is the Prolog code.
Now the program determines a solution almost immediately.
Here is the output of the Prolog code.
7. General method
The general method can be summarized as follows.
Determine the finite domains (sample spaces).
Determine the (ordered) constraints.
Generate the (ordered) possibilities using the permutation DSL.
Intersperse the constraints into the list of generated possibilities.