Function Repository Resource:

Alphametic

Source Notebook

Solve an alphametic puzzle

Contributed by: Peter Valko

ResourceFunction["Alphametic"][string]

finds a solution to the alphametic puzzle string or returns an empty list if no such exists.

Details and Options

ResourceFunction["Alphametic"][string,n] returns n solutions if possible.
ResourceFunction["Alphametic"][string,All] returns all solutions.
The string must contain one and only one equal sign.
Upper and lower case are treated as different letters.
The solver uses interval arithmetic and backtrack search.

Examples

Basic Examples (3) 

Solve the classic alphametic puzzle "send+more=money":

In[1]:=
ResourceFunction["Alphametic"]["SEND+MORE=MONEY"]
Out[1]=

This has a unique solution:

In[2]:=
Length[ResourceFunction["Alphametic"]["SEND+MORE=MONEY", All]]
Out[2]=

In this example, there are 1200 solutions:

In[3]:=
Length[ResourceFunction["Alphametic"]["POST-MORE=GELT", All]]
Out[3]=

If a puzzle has no solution, then an empty list is returned:

In[4]:=
ResourceFunction["Alphametic"]["WIRE*PLUS=WAMPUM", All]
Out[4]=
In[5]:=
% // Length
Out[5]=

Basic Examples (1) 

Parentheses and exponentiation operations are allowed:

In[6]:=
ResourceFunction["Alphametic"]["(RE+MI)^T+MO-RE=LUCRE"]
Out[6]=

Publisher

Peter Valko

Version History

  • 2.0.0 – 15 April 2020
  • 1.0.0 – 08 April 2020

Source Metadata

Related Resources

Author Notes

Dudeney's alphametic puzzle "SEND+MORE=MONEY" is widely known to have the solution 9567+1085=10652.
For our purpose, an alphametic problem is a string containing one and only one equal character separating two substrings. The substrings consist of words connected by arithmetic operations (+, -, *, /, ^). Parentheses are also allowed.
A character substitution rule is a one-to-one mapping of letters to digits in such a manner that no word starts with zero.
A particular character substitution rule is the solution of the puzzle if the two substituted substrings evaluate to the same numeric quantity. Often only the substituted form is given as the solution of the puzzle (see the first sentence of this paragraph).
A problem may have no solution, a unique solution or several solutions, but uniqueness is highly appreciated.
From a programming point of view, an alphametic—being a combinatorial problem—can be easily solved by total enumeration. The resource function Alphametic is more effective due to utilizing backtrack search and interval arithmetic.

License Information