Cryptographic problems are classic constraint satisfaction problems . Basically, you need to have your program generate constraints based on input, so that you end up with the following:
O + O = 2O = R + 10Carry1 W + W + Carry1 = 2W + Carry1 = U + 10Carry2 T + T + Carry2 = 2T + Carry2 = O + 10Carry3 = O + 10F
Generalized pseudocode:
for i in range of shorter input, or either input if they're the same length: shorterInput[i] + longerInput2[i] + Carry[i] = result[i] + 10*Carry[i+1]
Additional restrictions based on the definition of the problem:
Range(digits) == {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Range(auxiliary_carries) == {0, 1}
So for your example:
Range(O, W, T) == {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} Range(Carry1, Carry2, F) == {0, 1}
Once you have created restrictions to limit the search space, you can use the CSP resolution methods, as described in the related article, to go through the search space and determine your solution (if it exists, of course). The concept of (local) consistency is very important here, and using it can significantly reduce the search space for the CSP.
As a simple example, note that cryptocriticality generally does not use leading zeros, which means that if the result is longer than both inputs, the ending digit, i.e. the last portable digit, should be 1 (so in your example, this means F == 1 ). This restriction can then be extended backwards, as this means that 2T + Carry2 == O + 10 ; in other words, the minimum value for T should be 5, since Carry2 can be no more than 1 and 2 (4) + 1 == 9. There are other ways to improve the search (algorithm for minimal conflicts, etc.), but I would chose not to turn this answer into a full-fledged CSP class, so I will leave you a further investigation.
(Note that you cannot make assumptions such as A+C=A → C == 0 , except for at least a significant column due to the possibility of C being 9 and the carry digits in the column equal to 1. This means that C will generally be limited to {0, 9} , so you weren’t completely disconnected.)