Help Please

Dont know how to replace each letter as s number can someone help me ... this is the question i was given ...

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, thenT=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.
Why don't you try to write yourself that "simplest technique" about you say? I think it's better for you than to do someone this task.

EDIT:
I solved this puzzle: I wrote the program and I know the unique(!) solution: (T, O, G, D) = (4, 9, 1, 6)!
Last edited on
Let's come up with a helping hand: using numbers in base 10 we have the equation:

4*(100*T + 10*O + O) = 1000*G + 100*O + 10*O + D;

After reducing like terms we have:

1000*G - 400*T + 66*O + D = 0.

The program will have to test this equation is satisfied and that variables are different each other. That's all!
you could also just write

if(!(T == O || T == G || T == D || O == G || O == D || G == D))...

or declare a vector of exclusions that you push_back() with the value of each for loop and have another loop in each loop that checks if the value is in the exclusion vector, but I prefer the first technique... :-)

[edit] In fact, you need to initialise an exclusion vector like
std::vector<unsigned int> exclusions = {0, 9, 8, 1, 2, 5, 6, 7}
[/edit]
Last edited on
Thanks condor ... I thought there was a specific function i needed to replace each letter with a number but what you show there makes sense as in putting it in its slots in numerical value and adding it together ... Appreciate the help.
@scardoso

You're welcome! If you have some troubles with the code dare to ask us.
So I think I got it up until repeating numbers ... I can't figure out how to make it so it doesnt finish the loop if one of the numbers are repeated ... Any ideas?

#include <iostream>
#include <stdio.h>

using namespace std;

int main()

{

int T=0,O=0,G=0,D=0, answer=0, good=1;

while (answer != good && T < 9)
{
O = 0;

T++;

answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;

printf("second loop %i .... %i\n", answer, good);

while (answer != good && O < 9)
{
G = 0;

O++;

answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;

printf("third loop %i .... %i\n", answer, good);

while (answer != good && G < 9)
{
D = 0;

G++;

answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;

printf("fourth loop %i .... %i\n", answer, good);

while (answer != good && D < 9)
{

D++;

answer = 4 * (T * 100 + O * 10 + O);
good = G * 1000 + O * 100 + O * 10 + D;

printf("fifth loop %i .... %i\n", answer, good);
}}}}

printf("T = %i O = %i G = %i D = %i", T,O,G,D);

}
I found that there are 24 solutions to this puzzle, and none of them are Condor's...

@Condor Where do you get this 1000*, 100* stuff, how's it supposed to solve the puzzle?

Me, I applied the nested loop previously discussed, and just verified that the values were different from each other AND that the equation was satisfied...
it comes up to 24 solutions

[edit] @Condor ok... I get it... the letters become actual numbers... right... damn! I'll try again...
Last edited on
I end up with (T, O, G, D) = (1, 6, 0, 4) and (T, O, G, D) = (4, 9, 1, 6) indeed...
nice!

But I guess T and G can't be equal to zero... so there really is one solution
Last edited on
How did you show to by pass if the numbers are the same ones and still satisfy the equation @AeonFlux1212 and @Condor
Here's my solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main()
{	
	for(int t = 1; t < 10; ++t)//t can't be 0 (it is the MSD = Most Significant Digit)!
	for(int o = 0; o < 10; ++o)
	for(int g = 1; g < 10; ++g)//also g can't be 0 ! (MSD)
	for(int d = 0; d < 10; ++d)
	if((1000 * g - 400 * t + 66 * o +  d) == 0)//if equation is satisfied
	if(g != t && g != o && g != d && t != o && t != d && o != d)//different each other
	{																
		cout << "\nSolution" << ": T = " << t << ", O = " << o << 
		", G = " << g << ", D = " << d << "\n\n";
		cout << "Check: ";
		cout << " TOO + TOO + TOO + TOO = GOOD" << '\n';
		cout << "\t" << t << o << o << " + " << t << o << o <<
		" + " << t << o << o << " + " <<t << o << o << " = " <<
		g << o << o << d << '\n';
	}
	return 0;
}

Output:

Solution: T = 4, O = 9, G = 1, D = 6

Check:  TOO + TOO + TOO + TOO = GOOD
	499 + 499 + 499 + 499 = 1996
Last edited on
If you want another example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>

using namespace std;

int main()
{
    unsigned int T, O, G, D;

    unsigned int numberOfPermutations = 0;

    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(!(T == 0 || G == 0) && !(T == O || T == G || T == D || O == G || O == D || G == D) && 1000*G - 400*T + 66*O + D == 0)
                    {
                        cout << "T = " << T << ", O = " << O << ", G = " << G << ", D = " << D << endl;

                        numberOfPermutations++;
                    }
                }
            }
        }
    }

    cout << endl
         << "numberOfPermutations = " << numberOfPermutations << endl << endl;

    return 0;
}


It's in line 11 and 12 in condor's version and in line 19 in mine.
Last edited on
Thanks a lot guys awesome help ... definitely showed me a lot of tricks to narrow down my programming as well ... Thanks for the help @Condor and @@AeonFlux1212
@scardoso

You're welcome! If you consider all OK please mark this thread as Solved.
Topic archived. No new replies allowed.