I'm looking for a way to solve this crypt arithmetic problem of:
ROBERT + GERALD = DONALD
and potentially others as well, where each letter represents a digit.
How would you go about solving this by hand and how does that relate to solving it programmatically?
Thank you in advance
You can actually work this out as a sum:
and use basic mathematical knowledge.
For example, there's no carry possible in the right column (6) and we have
T + D = D
. That meansT
must be zero.Similarly, for column 5, there's no carry from column 6 and
R + L = L
meansR
is zero as well, and no carry to column 4.Same with column 4,
E + A = A
soE
is zero.So we now have:
From there, we can infer from columns 3 and 1 that
b==n
andg==d
and they (along witho/a/l/d
) can be any value since every digit is being added to zero so there is no chance of carry anywhere. So let's just make them all one:In fact, you could make them all zero and end up with
000000 + 000000 = 000000
.But that's hardly programming related, so let's make it so:
That will give you a whole host of solutions.
And, before you complain that you cannot have repeated digits, there is no solution if that's the case, since mathematically both
T
andR
must be zero, as shown in the original reasoning above. And you can prove this empirically with:which outputs no solutions.
Now
DONALD + GERALD = ROBERT
, that's a different matter but you can solve that simply by modifying the code above slightly, making theif
statement into:and you get the single solution: