A more effective approach to verbal arithmetic / alphaethetics?

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[]) {
        // Countint Variables
        int index_h = 0;
        int index_e = 0;
        int index_s = 0;
        int index_t = 0;
        int index_b = 0;

        // Start with h = 1 and increase until the else-if statement is true
        for(int h = 1; h <= 9; h++) { // h = 1, because first Symbol can't be zero
            index_h++;
                // Increase e so long until e equals h
                for(int e = 0; e <= 9; e++) {
                    index_e++;
                 if (e == h) {
                    continue;
                 }

                 // Increase s so long until s equals h or e
                 for(int s = 0; s <= 9; s++) {
                     index_s++;
                    if (s == h || s == e) {
                       continue;
                    }//end if

                    // Increase t so long until t equals h or e or s.
                    for(int t = 1; t <= 9; t++) { // t = 1, because 1st Symbol cant be zero
                        index_t++;
                      if(t == h || t == e || t == s) {
                         continue;
                      }// end if

                      // Increase b so long until b equals h, e, s or t.
                      for(int b = 1; b <= 9; b++) { // b = 1, weil das 1. Symbol nicht für eine 0 stehen darf
                          index_b++;
                          if (b == h || b == e || b == s || b == t) {
                              continue;
                          }// end if

                          // x = 100*h + 10*e + s
                          // y = 100*t + 10*h + e
                          // z = 1000*b + 100*e + 10*s + t
                          // Check if x+y=z, if true -> Print out Solution, else continue with the upper most loop
                          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");
                          }// end else if
                      }//end for
                    }//end for
                 }//end for
              }//end for
        }//end for
    }   
}

Solving this puzzle requires about 15,000 odd cycle cycles. This is a lot, in my opinion. Any pointers please?

: , , " " ? . . ? ? ?... , .

+5
7

: ( ) ? , :

  • B = 1
  • T 0 ( THE), S, E 0.
  • T = E + S% 10

, S, E, H, 9 * 8 * 8, 576. , H + T 9, .

. 3 , .

public class Puzzle {
  public static void main(String[] args) {
    for (int S = 1; S<10; S++) {
      for (int E = 1; E<10; E++) {
        if (S==E) continue; // all letters stand for different digits
        for (int H = 1; H<10; H++) {
          if (H==E || H==S) continue; // all letters stand for different digits
          checkAndPrint(S, E, H);
        }
      } // for
    } // for
  } // main

  private static boolean checkAndPrint(int S, int E, int H) {
    int T = (S + E) % 10;
    int S1 = (E + H) + (S + E) / 10; // calculates value for 'S' in 'BEST' (possibly + 10)
    if (S1 % 10 != S) return false;
    int E1 = H + T + S1 / 10; // calculates value for 'E' in 'BEST' (possibly + 10)
    if (E1 % 10 != E) return false;
    System.out.println(H + "" + E + "" + S + " + " + T + "" + H + "" + E + " = 1" + E + "" + S + "" + T);
    return true;
  }
}
+4

, : - JavaFX. Firefly [GitHub]

+2

, , S, E T. S + E% 10 T. S, E, T, E + H + (0 1, , S + E 9) = S ... .. ..

+1

, , . , , , , , , .

0

.

, "BEST". , "HES" , 987, "THE" X98, "THE" 698, 987 + 698 = 1685.

"THE" , THE 987, HES - 876 → 876 + 987 = 1863, , 1685, 1863 - "". , "B" 1 ( ..). BEST , 1023.

- :

for(i=102;i<=987;i++)
{
    for(j=1023-i;j<=(1863-i < 987 ? 1863-i:987);j++)
    {
        //check if the solution matches and doesn't have duplicate digits
    }
}

. , .

.

0

. Java .

, . , .

0

, , , Prolog. :

( ),

- , , , , : -)

- - .

An alternative that does not guarantee a result is the use of optimization techniques such as genetic algorithms. Guess a series of solutions and calculate how close they are to the correct solution, and then adjust them. You can get partial solutions using this method.

0
source

All Articles