Perhaps most of you know Send + More = Money. Well, I'm currently learning Java, and one of the exercises is the HES + THE = BEST solution.
Now, I can / should / should use if-for-while-do loops, nothing more. Although I am sure that there are different methods for solving it, this is not the moment that I am fulfilling. I should be able to use if-for-while-do in the most efficient way.
My problem? I do not seem to think of an effective way to solve it! I came up with this that solves the riddle, but perhaps the most effective way to do this is:
public class Verbalarithmetics {
public static void main (String args[]) {
int index_h = 0;
int index_e = 0;
int index_s = 0;
int index_t = 0;
int index_b = 0;
for(int h = 1; h <= 9; h++) {
index_h++;
for(int e = 0; e <= 9; e++) {
index_e++;
if (e == h) {
continue;
}
for(int s = 0; s <= 9; s++) {
index_s++;
if (s == h || s == e) {
continue;
}
for(int t = 1; t <= 9; t++) {
index_t++;
if(t == h || t == e || t == s) {
continue;
}
for(int b = 1; b <= 9; b++) {
index_b++;
if (b == h || b == e || b == s || b == t) {
continue;
}
else
if (100*h + 10*e + s + 100*t + 10*h + e == 1000*b + 100*e +10*s + t) {
System.out.println("HES + THE = BEST => " + h + e + s + " + " + t + h + e + " = " + b + e + s + t);
System.out.println("With H=" + h + ", E=" + e + ", S=" + s + ", T=" + t + ", und B=" + b + ".");
System.out.println("It took " + index_h +
" Loop-Cycles to find 'h' !");
System.out.println("It took " + index_e +
" Loop-Cycles to find 'e' !");
System.out.println("It took " + index_s +
" Loop-Cycles to find 's' !");
System.out.println("It took " + index_t +
" Loop-Cycles to find 't' !");
System.out.println("It took " + index_b +
" Loop-Cycles to find 'b' !");
System.out.println("This is a total of " + (index_h + index_e + index_s + index_t + index_b) +
" Loop-Cycles");
}
}
}
}
}
}
}
}
Solving this puzzle requires about 15,000 odd cycle cycles. This is a lot, in my opinion. Any pointers please?
: , , " " ? . . ? ? ?... , .