I am doing the EAT+ THAT= APPLE, where each letter represents a different number from 0-9. I need to find all combinations. I was wondering if there is a better way to write it, especially 'if' and 'for'
I've tried writing it like this but it gave me infinite results
public class Main {
public static void main(String[] args) {
int count = 0;
int E,A,T,P,L,H;
for (E = 0; E <=9; E++)
{
for (A = 0; A <=9; A++)
for (T = 0; T <=9; T++)
for (P = 0; P <=9; P++)
for (L = 0; L <=9; L++)
for (H = 0; H <=9; H++)
if (((E != A) && (E != L) && (E != T)&&(E !=P) &&(E!=L)&&(E!=H) &&
(T != A) && (T != L) && (T != E) &&(T!=P)&&(T!=L)&&(T!=H)))
{
System.out.println("A"+A+"P"+P+"P"+P+"L"+L+"E"+E);
}
else count = count +1;
}
System.out.println(count);
}
}
When facing such problems it is of great importance to simplify the problem as much as possible.
Let's create a sub problem:
Assume that
THAT
must equal 8208. What are the values of each character?You can notice that you are just solving an equation:
T
* 1000 +H
* 100 +A
* 10 +T
= 8208 (The only solution isT
= 8,H
= 2,A
= 0)Back to our main problem:
Using the logic above simplify the main problem to an equation,
EAT
+THAT
=APPLE
=>EAT
+THAT
-APPLE
= 0;That in fact means:
E
* 100 +A
* 10 +T
+T
* 1000 +H
* 100 +A
* 10 +T
-A
* 10000 -P
* 1000 -P
* 1000 -L
* 10 -E
= 0After simplification you get: -9980 *
A
- 1100 *P
+ 1002 *T
+ 100 *H
+ 99 *E
- 10 *L
= 0As the values for each variable are very limited we are free to brute force the solution.
If you don't want your if statement to be this massive, you can always create an array of size 10 filled with zeroes and for each variable (let's call it
i
) increase the value for the given index witharray[i]++;
, if at any point at any indexarray[x]
> 1 you will know that values repeat.There are ways of optimizing how the script works, by acknowledging the relations between digits (for example you can observe that
A
can only equal either 0 or 1, whereas 0 leads up to a contradictory equation, soA
must equal 1 and so on and so forth until you find the exact value for each digit just by using logic), but in the end you will end up with purely mathematical solution and I don't think that's what you want to end up with.