Java program to solve simple cryptarithmetic puzzle

10.8k views Asked by At

Edit: I got my program working now, but still need some clarification for the else if (400*T + 40*O + 4*O == 1000*G + 100*O + 10*O + D) part that is key to solving the puzzle. I just want to fully understand every bit of the program, thank you.

This is for review purposes only and I have spent a couple hours trying to figure it out. I'm either getting all zeroes for the variables or it's an infinite loop. Here is the question, as it's written in the book:

"In cryptarithmetic puzzles, mathematical equations are written using letters. Each letter can be a digit from 0 to 9, but no two letters can be the same. Here is a sample problem: SEND + MORE = MONEY A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y = 2, E = 5, N = 6, D = 7. Write a program that finds a solution to the cryptarithmetic puzzle of the following: TOO + TOO + TOO + TOO = GOOD The simplest technique is to use a nested loop for each unique letter (in this case T, O, G, D). The loops would systematically assign the digits from 0 to 9 to each letter. For example, it might first try T = 0, O = 0, G = 0, D = 0 then T = 0, O = 0, G = 0, D = 1 then T = 0, O = 0, G = 0, D = 2, etc., up to T = 9, O = 9, G = 9, D = 9. In the loop body, test that each variable is unique and that the equation is satisfied. Output the values for the letters that satisfy the equation."

public class PracticeProjectEight
{
    public static void main(String[] args)
    {
        int T = 0 , O = 0 , G = 0 , D = 0;
        boolean keepGoing = true;
        //boolean againT = true , againO = true , againG = true , againD = true ;

        // while (keepGoing)   
        //{
            for (T = 0 ;  T > 10 ; T++)  
            {    
                for (O = 0 ; O > 10 ; O++)  
                {    
                    for (G = 0 ; G > 10 ; G++) 
                    {    
                        for (D = 0 ; D > 10 ; D++)
                        {    
                            if ((D == G) || (D == O) || (D == T) || (G == O) || (G == T) || (O == T))
                            {
                                //keepGoing = true;
                                continue;
                            }

                            else if (400*T + 40*O + 4*O == 1000*G + 100*O + 10*O + D)
                            {      
                                //keepGoing = false;
                                System.out.println("T = " + T);
                                System.out.println("O = " + O);
                                System.out.println("G = " + G);
                                System.out.println("D = " + D);
                                System.exit(0);
                            }
                        }
                    }
                }      
            }
       //}
    }
}

As you can see, I tried to put all the for loops in a while loop to control everything as one of my many attempts to fix. It didn't work (at all), so I commented it out.

2

There are 2 answers

3
Dennis Meng On BEST ANSWER

All of those >s should be <s. You want < 10, not > 10.

In other words, it should be:

for (T = 0; T < 10; T++) {
    for (O = 0; O < 10; O++) { 
        for (G = 0; G < 10; G++) {
            for (D = 0; D < 10; D++) {
                // your logic as before
            }
        }
    }
}
0
AudioBubble On

Um, I'm new here, and new to programming in general. But I am working out of Absolute Java too, and just did this problem.

I haven't run your code, but just at a glance, there seem to be some problems.

(Or, perhaps I am mistaken. See for yourself. I'll just let you know what I'm thinking here.)

First, by the way you've structured it, it would not seem to allow you to discover MULTIPLE solutions to the problem. You tell the system to exit when a solution is reached, don't you? Or am I misunderstanding?

Second, you tell the program to "continue" if a solution is encountered where two of the variables correspond to the same numeral, but you don't tell the program NOT TO COUNT those solutions, which - I think - is what the programming problem requested.

Third, I'm not even sure what role "keep going" is playing. The nesting of the loops ensures - I think - that all possibilities/permutations are realized/considered by that point in the coding, does it not? So what is the point of the Boolean condition there?

I hold out the possibility that I am way off base in all of this. I have only been programming for 3 weeks. But here is my code (sorry so sloppy).

(Also, you'll see I threw a 'counter' in there to tally the combination/assignments that failed. But in this too I might be confused. Again, sorry if my comments are way off here.)

public static void main(String[] args) {

int count = 0;

for (int G = 0; G <=9; G++)   
{
    for (int O = 0; O <=9; O++)
        for (int T = 0; T <=9; T++)
            for (int D = 0; D <=9; D++)

    if (((G != O) && (G != T) && (G != D) && 
            (O != T) && (O != D) && (T != D)) 
            && 
            ((400 *T) + (40 * O) + (4*O)) == 
            ((1000*G) + (100*O) + (10*O) + (1*D)))
    {
        System.out.println("G = " + G + "," + " O = " + O + "," + 
                "T = " + T + "," + "D = " + D);

    }
    else count = count +1;    
}
System.out.println(count);