Assignment: Nested Loops

These can be tricky problems for many people. Start early!!!

PART A

[This is Problem 8 in Savitch's Absolute Java] In cryptarithmetic puzzles, mathematical equations are written using letters to represent digits. Each letter can be a digit from 0 to 9, but no two letters can be the same.The trick is to determine the value of each letter such that the mathematical equation is correct. Here is a sample problem:

SEND + MORE = MONEY

A solution to this puzzle is S=9, R=8, O =0, M=1, Y=2, E=5, N=6, D=7. This is because SEND = 9567, MORE = 1085, and MONEY = 10652, and 9567 + 1085 = 10652.

  1. Write a program (Crypta.java) that finds a solution to the cryparithmetic puzzle:
      TOO + TOO + TOO+ TOO = GOOD 
          
    You should use a series of four nested loops, each representing the value of one letter (T, O, G, D). The loops systematically assign a value from 0 to 9 to each of the letters. The loop controlling the value of D can be nested in the loop controlling the value of G, which in turn is nested inside the loop controlling the value of O, which is nested in the loop controlling the value of T. (The ordering of the loops is irrelevant.) In the body of the innermost of these loops, up to all 10,000 possible combinations of values of T, O, G and D will be tried. In the body of that innermost loop, if each of these values is distinct from the others and the equation is satisfied, output the value assigned to each letter.

    You will have to consider how to calculate the value of "GOOD" and "TOO" for given values of T, O, G and D. Your solution should include two public static int functions, good and too. good should take three int parameters--the value of "G", the value of O and the value of D--and should return the integer value of the string GOOD. For example, the function invocation good(5,1,3) should return the integer 5113. The function too should take two int parameters--the value of T and the value of O--and return the value of the string TOO. As hint, here is the header of too:

    public static int too(int valueOfT, int valueOfO)

    EXTRA CREDIT: Your program prints all solutions to the puzzle. (Yes, there is more than one!)

     

 

Submit your program to the drop box.